Use of static keywords for Object-Oriented Knowledge points
/* The attributes and methods defined by static are called static members and static methods. Static attributes and Methods belong to Classes and are shared among objects. */
/* For example, you can define a static variable to count the number of objects instantiated by the class */
Class test {
Static $ count;
Function _ construct () // defines a constructor.
{
Self: $ count ++;
}
Static function getCount () {// defines a static method and returns the value of the static variable $ count.
Return self: $ count;
}
}
Test: $ count = 0; // The initial static variable $ count is 0.
$ Test_01 = new test ();
$ Test_02 = new test ();
$ Test_03 = new test ();
$ Sum = test: getCount ();
Echo $ sum;
// Result 3
/*
You can use
Class Name: static member; // access static member
Class Name: static method; // access static method
Static Methods in the class can be passed through
Self: static member; // access static member
Self: static method; // access static method
Note: Only static members can be accessed in static methods.
*/
?>