Home > Backend Development > PHP Tutorial > Analysis of ideas on the usage of static classes and static variables in php

Analysis of ideas on the usage of static classes and static variables in php

黄舟
Release: 2023-03-15 10:56:01
Original
1198 people have browsed it

Analyze the difference between static classes and static variable usage classes in php, create the object $object = new Class(), and then use "->" to call: $object->attribute/function, provided that the variable/ Method is accessible. Directly call the class method: class::attribute/function, whether it is static or non-static. Static static: declare a class member or method as static, you can access it directly without instantiating the class, and you cannot access it through an object. Static members (except static methods) belong to the class and do not belong to any object instance, but object instances of the class can be shared.

First download the static classes and static variable usage libraries in PHP that we need to use in this course: http://www.php.cn/xiazai/leiku/610

Download After completion, find the php class file we need, unzip it to our local directory, and create a new php file!

After completion, we need to call this class in the new php file and instantiate the class:

<?php
include_once "person.php";//引入类文件
// 输出成员属性值
echo Person::$country."<br />";  // 输出:中国
$p1 = new Person();
//echo $p1->country;   // 错误写法
// 访问静态成员方法
Person::myCountry();   // 输出:我是中国人
// 静态方法也可通过对象访问:
$p1->myCountry();

// 子类中输出成员属性值
echo Student::$country."<br />"; // 输出:中国

$t1 = new Student();
$t1->study();    // 输出:我是中国人
?>
Copy after login

Run the file and the result will be as shown below:

Analysis of ideas on the usage of static classes and static variables in php

The above is the detailed content of Analysis of ideas on the usage of static classes and static variables in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template