PHP scope parsing operator (::)

Source: Internet
Author: User

Scope resolution operator (::)
I realized it only today when I read the joomla source code. In the past, this operator can also be used as a non-static method of the category class. It really surprised me. It has always been assumed that the scope parsing operator can only be the static method and static member variable of the category class.
If you don't believe it, there is a simple small test below Code This can be proved. Copy code The Code is as follows: Class {
Private $ _ name = 'a ';
Function _ construct (){
Echo 'a construct <br/> ';
}
Function Test (){
Echo 'a test () <br/> ';
}
}
Class B extends {
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 codeThe Code is as follows: A test ()
#########
B Test ()

although test () in Class A and test in Class B are not static methods, you can also use "Class Name: Method Name (parameter list). The effect is similar to that of the new class, and then the
test method is called using this instance.
however, if I need to print the name attribute in the test method, I can directly use: to call it. Then, let's first modify the above Code. copy Code the code is as follows: class A {
private $ _ name = 'a';
function _ construct () {
echo 'a construct
';
}< br> function test () {
echo 'a test ()
', $ this-> $ _ name, '
';
}< BR >}< br> Class B extends a {
private $ _ name = 'B ';
function _ construct () {
parent ::__ construct ();
echo 'B construct
';
}< br> function test () {
echo 'B test ()', $ this-> _ name, '
';
}< BR >}< br> A: Test ();
echo '#########
';
B: Test ();

the preceding code runs as follows: copy Code the code is as follows: Fatal error: using $ this when not in object context in D: \ www \ test \ scoperefe. PHP on line 9
[HTML]
some friends said that. You have not instantiated Class A at all. Of course, you cannot directly access the member variable $ _ name using the $ this-> _ name method. Then, do you want to change it to self :: $ _ name is enough?
just do what you want. Modify the above Code below
[Code]
Class A {
private $ _ name = 'a ';
function _ construct () {
echo 'a construct
';
}< br> function test () {
echo 'a test ()
', self: $ _ name,'
';
}< BR >}< br> Class B extends a {
private $ _ name = 'B';
function _ construct () {
parent ::__ construct ();
echo 'B construct
';
}< br> function test () {
echo 'B test ()', $ this-> _ name, '
';
}< BR >}< br> :: test ();
echo '########
';
B: Test ();

Run the above Code and the result is as follows:Copy codeThe Code is as follows: A test () fatal error: Access to undeclared Static Property: A ::$ _ name in D: \ www \ test \ scoperefe. php on line 9

The self keyword cannot be used to access non-static methods of the current class.
Now, if you want to call this method correctly, there are two methods:
1. First instantiate the class, and then use the object to call it directly using $ this-> _ name;
2. Set the member variable $ _ name to static;

We believe that all of the above problems can be handled correctly.

What I really want to say is:
If a method can be called without instantiation, we 'd better modify this method using the static keyword. Only static member variables of the class are called during implementation. In this way, the above problems will not occur.
If a method is not set to static. Therefore, it is safer to use instance objects for calling, because it may be necessary to modify the implementation of the method at any time. During the modification, it may be necessary to call
Non-static member variables (because, to a large extent, when implementing the modification method, you have forgotten to directly call such variables using the class name ).

Personal ignorance.

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.