In Java, static variables/static methods of classes are called class variables/class methods, they exist in the memory stack, only one copy, you can pass the class
and ObjectsDirect access
public class Mystatic {public static String className = "mystatic"; public static void GetClassName () { //Of course you write directly classname can also be accessed, but only if there is no local variable name in this function overrides the static variable name System.out.println ( mystatic.classname); } public static void Main (string[] args) { //class accesses static variable System.out.println (mystatic.classname); The object accesses the 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 variable System.out.println (mystatic.classname); The object accesses the static variable System.out.println ((New Mystatic ())). ClassName); Class Access static method 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