Static methods and static variables of PHP classes-php Tutorial

Source: Internet
Author: User
Static methods and static variables of PHP classes

A few days agoQingyuan educationWhen I see such an article on the website, I feel like I do.Php developersIt is particularly helpful. I am posting it now, hoping to help you. In additionQingyuan educationThere are many such technical articles on the website. if you are interested, you can check them out. Well, let's not talk much about it. let's start with the question:

In php, there are two methods for methods/variables of the callback class:
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.

I hope this article will be helpful to the majority of php developers. thank you for reading this article. MorePhp technical problemsWelcome to group discussion:256271784, Verification code:CxyIf you do not write the verification code, it will 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.