Use the scope resolution operator for PHP (::)

Source: Internet
Author: User
This article mainly introduces the use of PHP scope parsing operators (::), has a certain reference value, now share to everyone, the need for friends can refer to

Today to see the source of Joomla, just realize. It turns out that this operator can also access the non-static methods of the class. It really surprises me. Always assume that the scope resolution operator can only access the static methods and static member variables of the class.

Scope Resolution Operator (::)
Today to see the source of Joomla, just realize. It turns out that this operator can also access the non-static methods of the class. It really surprises me. Always assume that the scope resolution operator can only access the static methods and static member variables of the class.
If you don't believe it, here's a simple little test code to prove it.

Class a{Private $_name = ' a ', function __construct () {echo ' a construct <br/> ';} function test () {echo ' a test () <br/> '; }} class B extends a{private $_name = ' B '; function __construct () {parent::__construct (); Echo ' B construct <br/> '; } function Test () {echo ' B test () ';}} A::test (); Echo ' ######### <br/> '; B::test ();

The result of this code entry is:

A test () ######### B test ()

Although test in Class A is not a static method in Test () and Class B, it can be called correctly with the "Class Name:: Method Name (parameter list)" style. An instance of his effect and a new class, and then use this instance to invoke the
The test method is a sample.
However, if I need to print the Name property in the test method, it will be used directly with:: To invoke what the case is. Let's start by modifying the code above.

Class a{Private $_name = ' a ', function __construct () {echo ' a construct <br/> ';} function test () {echo ' a test () <br/> ', $this->$_name, ' <br/> '; }} class B extends a{private $_name = ' B '; function __construct () {parent::__construct (); Echo ' B construct <br/> '; } function Test () {echo ' B Test () ', $this->_name, ' <br/> ';}} A::test (); Echo ' ######### <br/> '; B::test ();

The above code runs with the following results:

Fatal error:using $this When not in object context in D:\www\test\scoperefe.php on line 9 [HTML]

That's what some friends say. You do not instantiate the Class A, of course, can not directly use the $this->_name way to access the member variable $_name, then, is not modified to self::$_name on the line?
Just do what you say, and change the code below.

[Code] class a{Private $_name = ' A '; function __construct () {echo ' a construct <br/> ';} function test () {echo ' A Test () <br/> ', self::$_name, ' <br/> '; }} class B extends a{private $_name = ' B '; function __construct () {parent::__construct (); Echo ' B construct <br/> '; } function Test () {echo ' B Test () ', $this->_name, ' <br/> ';}} A::test (); Echo ' ######### <br/> '; B::test ();

Then run the above code with the following results:

A test () Fatal error:access to undeclared static property:a::$_name in D:\www\test\scoperefe.php on line 9

Oh, the non-static method of the current class cannot be accessed with the Self keyword.
Now, if you want to call this method correctly, there are 2 practices:
1, the first instantiation of the class, and then use the object call can be directly used $this->_name to invoke;
2, set the member variable $_name to static;
The above question, I believe that everyone can be handled correctly.
In fact, what I really want to say is:
If a method can be called without instantiation, then we'd better use this method under the static keyword modifier. When implementing a method, only the static member variable of the class is called. This will not appear above the problem.
If a method does not have a method set to static. The safest approach, then, is to make the invocation of the instance object more secure, since it might be necessary to modify the implementation of the method whenever it is modified, perhaps by invoking the
Non-static member variables (because, to a large extent, the implementation of the method is modified, it has been forgotten that the class name is also called directly).
Personal humble opinion.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.