What is the difference and usage between static attributes and ordinary attributes in object-oriented PHP
What is the difference and usage between static attributes and ordinary attributes in PHP object-oriented
<code>区别: 1、静态属性、静态方法是属于类的,是所有对象共有的,不属于任何一个具体的对象; 2、普通属性、普通方法是属于一个具体的对象的。 用法: 1、类外部 类名::属性名、类名::方法名() 的形式访问; 2、类内部 self::属性名、self::方法名() 的形式访问。 class Test { // 静态属性 private static $name = '测试'; // 静态方法 public static function getName () { return self::$name; } } // 外部调用静态方法 $name = Test::getName(); // 测试</code>
The so-called static means that the address of this member in the memory will not change.
Then the static method of the class can be called directly without instantiating the object, because if the object is instantiated, its address will be reallocated every time an object is instantiated, then this object is in the memory The address in is dynamic.
After knowing this, you can understand that static members have their location in memory fixed when the class they belong to is loaded.
So static members do not instantiate objects, but call them directly using the class name.