Introduction to static (static) methods and static (static) variables of PHP classes

Source: Internet
Author: User
The static and static variables of the PHP class are introduced. if you are a friend of php, you can see that in php, the methods/variables of the static class have two methods:
1. create an object $ object = new Class () and then use "->" to call: $ object-> attribute/function, provided that the variable/method is accessible.
2. directly call the class method/variable: class: attribute/function, either static or non-static. However, there are prerequisites:
A. If it is A variable, the variable must be accessible.
B. If it is a method, in addition to the method that can be accessed, it also needs to meet the following requirements:
B1) if it is a static method, there are no special conditions;
B2) if it is a non-static method, $ this is not used in the method to be modified, that is, non-static variables/methods are not called. of course, there is no problem in calling static variables/methods.

Then let's take a look at using $ object->... And use class ::... Are there any differences:
1. use $ object->... , You need to execute the constructor to create an object;
2. use class ::... Call static methods/variables without executing constructors to create objects;
3. use class ::... Call non-static methods/variables, and do not need to execute constructors to create objects.

Then it came out in a strange way. since both 2 and 3 are the same, what is the significance of static methods/variables?
The difference still exists as follows:
1. static variables
Static members only retain one variable value, which is valid for all instances, that is, all instances share this member.
2. static method
Static methods can directly use class ::... Instead of static methods, you must meet certain conditions to use the class:... method call, as described earlier

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.