Static variables/static methods of classes in Java are also called class variables/class methods, they exist in the memory stack, only one copy, can be directly accessed through classes and objects
Public class mystatic { public static string classname = "Mystatic"; public static void getclassname () { //Of course you can also access the direct write classname, but only if there is no local variable name in this function override static variable name system.out.println (Mystatic.classname); } public static void main (String[] args) { //class Access static variables System.out.println (mystatic.classname); //object access static variable system.out.println ((New mystatic ()). ClassName); //class Access static method mystatic.getclassname () ; //object Access static method (New mystatic ()). GetClassName (); //========= (New mystatic ()) .classname = "New class name"; //========= //class Access static variables system.out.println (mystatic.classname); //object access static variable system.out.println ((New mystatic ()). ClassName); //class Access static methods Mystatic.getclassname (); //object access static method (New mystatic ()). GetClassName (); }}
Note that Java static variables do not prohibit access to the object, but PHP is different, the static method of PHP can be called by the object, but the static variables of the class can only be accessed by the method of the class, the object is not directly accessible
Class Mystatic {public static $className = __class__; public static function GetClassName () {echo self:: $className; }}echo mystatic:: $className; Mystatic::getclassname ();//but (new Mystatic ())->classname;//error No permissions//but below is Possible (new Mystatic ()) GetClassName ();//static only one copy, everyone together hard connection, synchronous modification mystatic:: $className = "New Class Name";(new Mystatic ())->getclassname ();
So the PHP object must rely on the interface to access the static variables of the class, and write a method of accessing the static variable in the class, rather than using it directly like Java. syntax classes and objects are accessible
PHP static vs. Java static