First, static properties
A static property cannot be accessed by an object through the operator.
A static property can only be initialized to literal or constant, and cannot be used with an expression. You can initialize a static property to an integer or an array, but you cannot initialize it to another variable or function return value, or point to an object.
How do I access static properties?
Inside the class: Class Name:: $ static property or self::$ static property
Outside the class: class Name:: $ static Property or object instance:: $ static property
classperson{Public Static$nums =0;//declaration of static Properties Publicfunction Addnum () {self:: $nums++; echo self:: $nums; //inside the class, use the Self keyword to access the static propertyecho Person:: $nums;//class can access static properties directly//Echo $this->nums; //error, static properties cannot be accessed by the object by operator}} $p 1=NewPerson (); Echo $p 1:: $nums; //object instances can access static properties like this
Second, static method
Static methods are used to manipulate static properties. Therefore, non-static properties cannot be manipulated inside a static method.
because static methods do not need to be called through an object, the pseudo-variable $this in a static method does not available.
How do I access static methods?
Inside the class: class Name:: Static method or Self:: Static method
Outside the class: class Name:: Static method or object instance-static method
classperson{ Public Static$nums =0;//declaration of static Properties Public$age =0; Publicfunction Addnum () {self:: $nums++; echo self:: $nums; //inside the class, use the Self keyword to access the static propertyecho Person:: $nums;//class can access static properties directly//Echo $this->nums; //error, static properties cannot be accessed by the object by operatorSelf::getnum ();//inside the class, use the Self keyword to access the static methodPerson::getnum ();//class can access static methods directly } Public Staticfunction Getnum () {//Static Methods//Echo $this->age; //error, static method inside cannot manipulate non-static propertyEcho'Are you sure? '. Self:: $nums;//Static properties can only be manipulated in static methods}} $p 1=NewPerson (); $p 1-addnum (); Echo $p 1:: $nums; //object instances can access static properties like thisPerson::getnum ();//class can access static methods directly$p 1->getnum ();//object instances can access static methods like this
static properties and static methods for PHP static