This article mainly introduces the usage of static and const keywords in php, and analyzes the functions, usage and related precautions of static and const keywords in the form of examples. Friends in need can refer to it
The details are as follows:
The member properties and member functions described by the static keyword in the class are all static.
Static members can restrict external access, because static members belong to the class, not to any object instance.
From a memory perspective, the object is placed in "heap memory", the reference to the object is placed in "stack memory", and the static members are placed in the initialization static segment. Added when loading. Can be shared by all objects in memory. As shown in the figure below:
##
<?php class Person{ public static $myCountry = "中国"; public static function say(){ echo "我的祖国是:".self::$myCountry."<br>"; } } //输出静态属性 echo Person::$myCountry."<br>"; //调用静态方法 Person::say(); //修改静态属性 Person::$myCountry = "中国-江苏"; echo Person::$myCountry."<br>"; ?>
中国 我的祖国是:中国 中国-江苏
<?php class MyClass{ const constant = 'constant value'; function showConstant(){ //方法中调用常量,没有$ echo self::constant."<br>"; } } //类直接调用,没有$ echo MyClass::constant."<br>"; $class = new MyClass(); $class ->showConstant(); ?>
php Multiple inheritance Several methods
php-fpm.conf configuration instructions
The above is the detailed content of Usage of static and const keywords in php. For more information, please follow other related articles on the PHP Chinese website!