Home >Backend Development >PHP Problem >What is the difference between php static methods and instantiated methods

What is the difference between php static methods and instantiated methods

WBOY
WBOYOriginal
2022-03-14 11:20:542135browse

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.

What is the difference between php static methods and instantiated methods

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

What is the difference between static methods and instantiation methods in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn