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. 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 code below to prove this.
The code is as follows:
Class {
Private $ _ name = 'a ';
Function _ construct (){
Echo 'a construct
';
}
Function test (){
Echo 'a test ()
';
}
}
Class B extends {
Private $ _ name = 'B ';
Function _ construct (){
Parent: :__ construct ();
Echo 'B construct
';
}
Function test (){
Echo 'B test ()';
}
}
A: test ();
Echo '#########
';
B: test ();
The result of this code entry is:
The code is as follows:
A test ()
#########
B test ()
Although test () in Class A and test in class B are not static methods, you can use the style "class name: method name (parameter list)" to call the method correctly. And the new class instance, and then use this instance to call
The test method is similar.
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.
The code is as follows:
Class {
Private $ _ name = 'a ';
Function _ construct (){
Echo 'a construct
';
}
Function test (){
Echo 'a test ()
', $ This-> $ _ name ,'
';
}
}
Class B extends {
Private $ _ name = 'B ';
Function _ construct (){
Parent: :__ construct ();
Echo 'B construct
';
}
Function test (){
Echo 'B test ()', $ this-> _ name ,'
';
}
}
A: test ();
Echo '#########
';
B: test ();
The running result of the above code is as follows:
The code is as follows:
Fatal error: Using $ this when not in object context in D: \ www \ test \ scoperefe. php on line 9
[Html]
That's what some friends said. 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 it. modify the above code below.
[Code]
Class {
Private $ _ name = 'a ';
Function _ construct (){
Echo 'a construct
';
}
Function test (){
Echo 'a test ()
', Self: $ _ name ,'
';
}
}
Class B extends {
Private $ _ name = 'B ';
Function _ construct (){
Parent: :__ construct ();
Echo 'B construct
';
}
Function test (){
Echo 'B test ()', $ this-> _ name ,'
';
}
}
A: test ();
Echo '#########
';
B: test ();
Run the above code and the result is as follows:
The 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.