Home>Article>Backend Development> The difference between php dynamic methods and static methods

The difference between php dynamic methods and static methods

(*-*)浩
(*-*)浩 Original
2019-10-09 09:33:53 3394browse

The difference between php dynamic methods and static methods

#Static methods are stored in memory throughout the application, which is fast but takes up memory.(Recommended learning:PHP video tutorial)

class A { public static string b() { return "Hello"; } }

Usage:

A.b(); //调用方便

Dynamic methods declare class instances first Only methods in the class can be called.

class A { public string b() { return "Hello"; } }

Usage:

A a = new a(); a.b();

Generally, static methods are used for frequently used methods, and dynamic methods are used for rarely used methods. Static is fast and takes up memory. The dynamic speed is relatively slow, but after the call is completed, the class is released immediately, which can save memory. You can choose whether to use the dynamic method or the static method according to your own needs.

The main problem with static methods is data synchronization. If you don't save private variables in the class of your static method, then there won't be any problems. It is best to pass all the data to be operated on to the method in the form of parameters

Static methods are class methods and do not need to create a class instance when calling.

Static methods are statically bound to subclasses, not inherited.

Static methods modify the state of the class, while objects modify the state of each object. This is also an important difference between them

The instantiation call of a class exists in the life cycle of the class. When the class is gone, the corresponding instance will be gone, and the corresponding method will be gone. Otherwise, as long as you reference the namespace of the static class , it will exist until you exit the system.

The above is the detailed content of The difference between php dynamic methods and static 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
Previous article:How to view pictures in php Next article:How to view pictures in php