Difference: 1. Static methods can be called directly without the need to open space and other operations, while instance methods need to open space; 2. Static methods share a space and the same data, while instance methods do not share the same space. and data; 3. Static methods do not support chain writing, but instance methods support chain writing.
The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.
Static methods only occupy one copy of the memory and are created when the code is loaded. Instantiation methods or classes are only created when new Just created. Each instantiated object represents a different instance, and there is only one static copy. Pay special attention to the fact that static methods share resources in multi-threads.
Difference:
1. Static method call does not require new, class name::method name.
For example:
User::find();
Note: Non-static properties cannot be called in static methods.
Instance methods require new.
For example:
12$userObj = new User;$userObj->find();
2. There is only one copy of static methods in memory, and resources are shared within a PHP life cycle.
Note: Static methods and properties are loaded as the class is loaded, so too many static methods will consume more memory.
Every time an instance method is new, an independent space will be opened up, that is, there will be multiple copies in the memory.
3. Static method performance: direct call, no need to open space and other operations, better in terms of time and efficiency
Instance methods require some time to open space and other operations
4. Static methods share the same space and the same data. In some scenarios, static methods are more suitable.
Example method Multiple instances do not share the same space and data
5. Static Method chain writing does not support
instance method support. Such as:
1$userObj->fields('uid')->where('uid>0')->find();
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between php static methods and instantiated methods. For more information, please follow other related articles on the PHP Chinese website!