What are the differences between static calls and instantiated calls of PHP classes?

烟雨青岚
Release: 2023-03-01 19:22:01
Original
3308 people have browsed it

The differences are: 1. Static methods generate memory when the program starts, and instance methods generate memory while the program is running; 2. Static methods can be called directly, and instance methods must first generate an instance, and then call the method through the instance. ; 3. Static memory is continuous, and instances apply for discrete space, so it is not as fast as static methods, and static memory is limited.

What are the differences between static calls and instantiated calls of PHP classes?

There is only one static method. No matter how many times you call it, it is shared. There is no concept of object, so it cannot be used statically. Use $this to call in the method. If you have to call, you can only instantiate your own class

Instancing is different. Each instantiation is an object and has multiple.

Let’s use an example to understand

 foo();//正确 /*   bar()是一个非静态方法,应该由对象来调用,但用静态来调用此方法也可以执行,而严格状态下,此方法会执行,同时报错,   Strict Standards: Non-static method Human::easyeat() should not be called statically in........   但是实例化的类是可以调用静态方法的。 */
Copy after login

        
Copy after login

Make a short summary:

The biggest difference is memory.

Static methods generate memory when the program starts, instance methods generate memory while the program is running,

Sostatic methods can be called directly, instances The method must first generate an instance and call the methodthrough the instance. The static speed is very fast, but if it is too much, it will occupy memory.

Any language operates on memory and disk. As for whether it is object-oriented, it is just a question of the software layer. The bottom layer is the same, but the implementation method is different.

Static memory is continuous because it is generated at the beginning of the program, and the instance applies for discrete space, so of course it is not as fast as the static method,

Andstatic memory is limited, if there is too much, the program will not be able to start.

Recommended tutorial: "php tutorial"

The above is the detailed content of What are the differences between static calls and instantiated calls of PHP classes?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!