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 ~
|