PHP Basics: Class and Object (4) Scope resolution operators (::) _php Tips

Source: Internet
Author: User
Tags php basics
The range 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 you access these static members, methods, and constants outside of the class, you must use the name of the class.
Putting Paamayim Nekudotayim in Hebrew is the meaning of double colons.

To use outside of a class:: operator
Class MyClass {
Const Const_value = ' A constant VALUE ';
}

Echo Myclass::const_value;

The two special keywords, self and parent, are used to access members or methods within a class.

Example:
Class Otherclass extends MyClass
{
public static $my _static = ' static var ';

public static function Doublecolon () {
Echo Parent::const_value. "N";
echo Self:: $my _static. "N";
}
}
Otherclass::d Oublecolon ();

When a subclass overrides a method in its parent class, PHP no longer executes the overridden methods in the parent class until the methods are called in the subclass. (Is it nonsense? No, this is a little different from other mainstream languages). This mechanism is also used for constructors and destructors, overloading and magic functions.

Class MyClass
{
protected function MyFunc () {
echo "Myclass::myfunc () n";
}
}

Class Otherclass extends MyClass
{
Overriding methods in the parent class
Public Function MyFunc ()
{
But you can still invoke methods that have been overridden
Parent::myfunc ();
echo "Otherclass::myfunc () n";
}
}

$class = new Otherclass ();
$class->myfunc ();




--------------------------------------------------------------------------------
?? Focus?? Be sure to use the class name when accessing static methods or members:: By the way.
Also note that PHP does not actively invoke methods of the parent class, including constructors and destructors.
See this text: http://163xiaofan.blog.163.com/blog/static/1713578020061027101820973

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.