Home > Backend Development > PHP Tutorial > php中的parent、self、static关键字

php中的parent、self、static关键字

WBOY
Release: 2016-06-23 13:27:49
Original
1185 people have browsed it

1. parent关键字可以用于调用父类中被子类重写了的方法


2. self关键字可以用于访问类自身的成员方法,也可以用于访问自身的静态成员和类常量;不能用于访问类自身的属性;使用常量的时候不需要再属性面前加$符号


3. static关键字用于访问类自身定义的静态成员,防伪静态属性时需要在属性面前添加$符号


<?phpclass BaseClass{	public function test(){		echo "BaseClass::test called\n";	}	public function test1(){		echo "BaseClass::test1 called\n";	}}class ChildClass extends BaseClass{	const CONST_VALUE = "A constant value\n";	private static $sValue = "static value\n";	public function  test(){		echo "ChildClass::test called\n";		parent::test();		//使用parent关键字可以访问父类中被子类重写的方法		self::called();		echo self::CONST_VALUE;		echo self::$sValue;		//使用self关键字可以访问自身的成员方法和常量成员	}	public function called(){		echo "ChildClass::called() called\n";	}}$obj = new ChildClass();$obj->test();
Copy after login



版权声明:本文为博主原创文章,未经博主允许不得转载。

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template