Home > Article > Backend Development > Basic accounting exercises and case answers Basic PHP knowledge: Classes and objects 4 Range resolution operator::
The scope resolution operator (also known as Paamayim Nekudotayim) or more simply a pair of colons can be used to access static members, methods and constants, and can also be used to access members and methods in overridden classes.
When accessing these static members, methods and constants outside the class, the name of the class must be used.
Paamayim Nekudotayim means double colon in Hebrew.
Use :: operator outside the class
class MyClass {
const CONST_VALUE = 'A constant value';
}
echo MyClass::CONST_VALUE;
self and parent These two special keywords are used in the class Internal access to members or methods.
Example:
class OtherClass extends MyClass
{
public static $my_static = 'static var';
public static function doubleColon() {
echo parent::CONST_VALUE . " n";
e cho self::$my_static . " n
. (Is it nonsense? No, this is a little difference between PHP and other mainstream languages). This mechanism also works for constructors and destructors, overloads, and magic functions.
class MyClass
{
protected function myFunc() {
echo "MyClass::myFunc() n";
}
}
class OtherClass extends MyClass
{
// Override methods in parent class
public function myFunc()但 {
// but you can still call the covered method
Parent :: myfunc ();
echo "OtherClass :: myfunc () n"; ->myFunc();
------------------------------------------------ -------------------------------------
??Key point??When accessing static methods or members Be sure to use the class name:: method.
Another note: PHP will not actively call the methods of the parent class, including constructors and destructors.
See this text: http://163xiaofan.blog.163.com/blog/static/1713578020061027101820973
The above introduces the basic accounting exercises and case answers. PHP basic knowledge: Classes and Objects 4 Range Analysis Operator::, including basic accounting exercises and case answers. I hope it will be helpful to friends who are interested in PHP tutorials.