Introduction to static and static variables for PHP classes _php tutorial

Source: Internet
Author: User
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

http://www.bkjia.com/PHPjc/324956.html www.bkjia.com true http://www.bkjia.com/PHPjc/324956.html techarticle in PHP, there are two ways to access a class's methods/variables: 1. Create Object $object = new Class (), and then use "-" Call: $object-attribute/function If the variable/method can be accessed ...

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