PHP between static methods and non-static method call each other

Static method call non-static methods: the static method in the class, need to instantiate an object, and then calls the class method, the following sample code:

class T {
    public function func () {     
        return 's to ns' ;
    }  
    public static function action () {
        return (new self())->func();
    }
}

Non-static method static method call: can selfor class names ::in the form of calls
reasons not directly call:

  • Static methods belong to the class, as the class is loaded and loaded, the program loads the class when the memory has been allocated for the static method
  • Non-static method belongs to the object, the object is then created in Lei Jia
  • Static method before the object exists, when an object is created, the program memory is allocated, this points to an object pointer access. Static method does not rely on calls to the object, it is through 类名::静态方法名this way to call. For non-static methods, it will allocate memory for when the program object is created, and then through the object to access non-static method
  • When the object is not present, there is no non-static methods, static methods will not call a method that does not exist
Published 80 original articles · won praise 96 · views 360 000 +

Guess you like

Origin blog.csdn.net/Alen_xiaoxin/article/details/104734370