OOP-When do PHP projects use static properties?

Source: Internet
Author: User
In general, we do the PHP project class within the definition of methods or properties are ordinary when to use static methods and properties have any examples of

I rarely use static properties for once. I used to do it when I was in the membership center to get the membership menu.

private static $menu = array();

Can we talk about this?

Reply content:

In general, we do the PHP project class within the definition of methods or properties are ordinary when to use static methods and properties have any examples of

I rarely use static properties for once. I used to do it when I was in the membership center to get the membership menu.

private static $menu = array();

Can we talk about this?

When a method is not related to an instance, that is, when you want to get rid of the constraints of instantiation directly using a method or property in a class, you can define it as static .
Also on the efficiency:
Class name Access static methods > instance access static methods > instance access non-static member methods

However, the excessive definition of static methods or properties will undoubtedly have an impact on the encapsulation and security of the class.

btw, it is recommended that the attribute be defined as a constant if the relationship to the instance is almost 0 o'clock const .

In a class, something unchanging can use static, such as a DSN for a database link, and a membership menu as mentioned in your question.

I generally do not exclude the business code with static methods/properties, but the framework/public code is still hard to avoid static methods and properties (a few cases such as factory methods can be excluded), because this involves

    • Extensibility callers writing the dead class name means that changing the behavior can only change the code of the class and cannot override it through inheritance.
    • Testability static methods/properties are often difficult to insert piles when testing
    • The coupling degree is greatly increased when the coupling-specific static property can be modified externally. public static properties have modified behavior, and global variables are almost only the difference between the name, the degree of disaster is the same
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.