The comparison between PHP static and Java static is also called class variables/class methods in Java. they exist in the memory stack and have only one copy.
And objectDirect access
Public class MyStatic {public static String className = "MyStatic"; public static void getClassName () {// of course you can also access it by writing className directly, the premise is that no local variable name in this function overwrites the static variable name System. out. println (MyStatic. className);} public static void main (String [] args) {// class access static variable System. out. println (MyStatic. className); // object access static variable System. out. println (new Mystatic ()). className); // class access static method Mystatic. getClassName (); // static method for object access (new Mystatic ()). getClassName (); // ========= (new Mystatic ()). className = "new class name"; // ============/// class access static variable System. out. println (MyStatic. className); // object access static variable System. out. println (new Mystatic ()). className); // class access static method Mystatic. getClassName (); // static method for object access (new Mystatic ()). getClassName ();}}
Note: java static variables do not prohibit object access. However, unlike PHP, PHP static methods can be called by objects, but static variables of classes can only be accessed by class methods, objects cannot be accessed directly.
Class Mystatic {public static $ className = _ CLASS __; public static function getClassName () {echo self ::$ className ;}} echo Mystatic ::$ className; Mystatic :: getClassName (); // but (new Mystatic ()-> className; // error does not have permission // but the following is acceptable (new Mystatic ()) -> getClassName (); // only one static copy is available. you can connect them together and modify Mystatic: $ className = "new class Name"; (new Mystatic () synchronously ()) -> getClassName ();
Therefore, PHP Objects must rely on interfaces to write out methods for accessing static variables in the class if they are to be used for static variables of the runtime class, rather than using the. syntax class and objects as in Java.