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

The difference between php dynamic methods and static methods

(*-*)浩
Release: 2023-02-25 20:48:01
Original
3485 people have browsed it

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";
        }
    }
Copy after login

Usage:

A.b();      //调用方便
Copy after login

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

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

Usage:

  A  a  =  new  a();      
  a.b();
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template