static methods and static variables for PHP classes

Source: Internet
Author: User
A few days ago inQingyuan EducationWeb site to see such an article, feel the same as IPHP DeveloperEspecially helpful, now posted out, hoping to help everyone, in additionQingyuan EducationThere are a lot of such technical articles on the website, and we are interested to have a look. Well, don't say much, next go to the point:

In PHP, there are two ways to access a class's methods/variables:
1. Create the object $object = new Class (), and then use the "-and" call: $object->attribute/function, provided the variable/method is accessible.
2. Call the class method/variable directly: class::attribute/function, either static/non-static. But there are prerequisites:
A. If it is a variable, it needs to be accessible.
B. In the case of a method, in addition to the method's accessibility, it is necessary to satisfy:
B1) If it is a static method, there is no special condition;
B2) If the method is non-static, it is not necessary to use $this, that is, the non-static variable/method is not called, of course, there is no problem in calling the static variable/method.

Then we'll look at the use of $object-> and use class:: ... What is the difference:
1. Use $object->.., you need to execute the constructor to create the object;
2. Use class:: ... Call static method/variable, do not need to execute constructor to create object;
3. Use class:: ... Call a non-static method/variable and do not need to execute the constructor to create the object.

Then the strange place came out, since 2 and 3 are the same, what is the meaning of static method/variable existence?
The difference is still obvious, as follows:
1. Static variables
A static member retains only one variable value, and this variable value is valid for all instances, that is, all instances share this member.
2. Static methods
Static methods can be used directly with the class:: ... Instead of static methods that require certain restrictions to use class::. Method calls, as described earlier.

I hope this article to the vast number of PHP developers to help, thank you for reading this article. MorePHP Technical IssuesWelcome to Dabigatran Discussion:256271784, Verification Code:Cxy, do not write verification does not pass yo ~

  • 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.