search
Homephp教程php手册php基础知识:类与对象(4) 范围解析操作符(::)

php基础知识:类与对象(4) 范围解析操作符(::)

Jun 13, 2016 pm 12:35 PM
phpandbasic knowledgeobjectoperatekindscopeparse

范围解析操作符(也可称作 Paamayim Nekudotayim)或者更简单地说是一对冒号,可以用于访问静态成员、方法和常量,还可以用于访问被覆盖类中的成员和方法。 
当在类的外部访问这些静态成员、方法和常量时,必须使用类的名字。
把 Paamayim Nekudotayim 在希伯莱文就是双冒号的意思。 

在类的外部使用 :: 操作符
class MyClass {
   const CONST_VALUE = 'A constant value';
}

echo MyClass::CONST_VALUE;

self 和 parent 这两个特殊的关键字是用于在类的内部对成员或方法进行访问的。 

例子:
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::doubleColon();

当一个子类覆盖其父类中的方法时,PHP 不会再执行父类中已被覆盖的方法,直到子类中调用这些方法为止。(是不是废话?不是,这是PHP与其他主流语言的一点区别)。这种机制也作用于构造函数和析构函数、重载及魔术函数。 

class MyClass
{
   protected function myFunc() {
       echo "MyClass::myFunc() n";
   }
}

class OtherClass extends MyClass
{
   // 覆盖父类中的方法
   public function myFunc()
   {
       // 但仍然可以调用已被覆盖的方法
       parent::myFunc();
       echo "OtherClass::myFunc() n";
   }
}

$class = new OtherClass();
$class->myFunc();




--------------------------------------------------------------------------------
??重点??访问静态方法或者成员时一定要使用类名::的方式。
另外就是注意:PHP不会主动调用父类的方法,包括构造函数和析构函数在内。
参见这个文字:http://163xiaofan.blog.163.com/blog/static/1713578020061027101820973 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools