PHP Scope resolution operator (::) _php Tips

Source: Internet
Author: User
Scope Resolution Operator (::)
Today to see the Joomla source, just realized. The original operator can also access the class Non-static method ah. It really amazes me that it's not good. The scope resolution operator has been assumed to have access only to the static method and static member variables of the class.
If you don't believe it, here's a simple little test code to prove it.
Copy Code code as follows:

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:
Copy Code code as follows:

A Test ()
#########
B Test ()

Although Test in Class A () and Class B are not static methods, you can use the style of the class name:: Method Name (argument list) to make the correct call. His effect and the new instance of a class, and then use this instance to invoke the
The test method is the same.
However, if I need to print the Name property in the test method, use:: To invoke what would be the case. Let's start by modifying the code above.
Copy Code code as follows:

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 results of the above code run are as follows:
Copy Code code as follows:

Fatal error:using $this When isn't in the object context into D:\www\test\scoperefe.php on line 9
[HTML]
That's what friends say. You have no instantiation 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 it, and then revise 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, and the results are as follows:
Copy Code code as follows:

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


Oh, you can't use the Self keyword to access the current class's non-static method.
Now, if you want to call this method correctly, there are 2 ways to do this:
1, first instantiate the class, and then use the object call can be directly used $this->_name to make the call;
2, the member variable $_name set to static;

The above question, I believe we all can handle correctly.

Actually what I really want to say is:
If a method can be invoked without instantiating it, then we'd better use the static keyword to modify this method. When implementing a method, only the static member variable of the class is invoked. So there's no problem.
If a method is not set to static. The safest approach, then, is to make the invocation of an instance object more secure, because it might be necessary to modify the implementation of the method, and when it is modified, it might be necessary to call the
A non-static member variable (because, to a large extent, when modifying the implementation of a method, it has been forgotten that a useful class name is called directly).

Personal humble opinion.

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.