Differences between php static methods and static variables

Source: Internet
Author: User
Differences between php static methods and static variables
This article introduces the differences between static and static variables in php classes. if a non-static method is used, $ this is not used in the change method, that is, non-static variables/methods are not called, of course, there is no problem in calling static variables/methods.

RelatedPhp static method and static variable usage.

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

Let's take a look at the differences between using $ object->... and using class:

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.

Since both 2 and 3 are the same, what is the significance of static methods/variables? Differences: 1. static variable static members retain only one variable value, and this variable value is valid for all instances, that is, all instances share this member. 2. static methods can be directly used class ::... class ::.. 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.