当前位置:Gxlcms > PHP教程 > php面向对象(OOP编程)-类访问修饰符

php面向对象(OOP编程)-类访问修饰符

时间:2021-07-01 10:21:17 帮助过:17人阅读

类型的访问修饰符允许开发人员对类成员的访问进行限制,这是PHP5的新特性


private
protected public
同一个类中
类的子类中
所有的外部成员

public;		echo $this->protected;		echo $this->private;	}}$obj = new MyClass();echo $obj->public;		// Worksecho $obj->protected;	// Fatal Errorecho $obj->private;		// Fatal Error$obj->printHello();		// Shows Public, Protected and Private/** * Define MyClass2 */class MyClass2 extends MyClass{	// We can redeclare the public and protected method, but not private	protected $protected = 'Protected2';	function printHello()	{		echo $this->public;		echo $this->protected;		echo $this->private;	}}$obj2 = new MyClass2();echo $obj->public;		// Worksecho $obj2->private;	// Undefinedecho $obj2->protected;	// Fatal Error$obj2->printHello();	// Shows Public, Protected2, not Private?>



MyPublic();		$this->MyProtected();		$this->MyPrivate();	}}$myclass = new MyClass;$myclass->MyPublic();		// Works$myclass->MyProtected();	// Fatal Error$myclass->MyPrivate();		// Fatal Error$myclass->Foo();			// Public, Protected and Private work/** * Define MyClass2 */class MyClass2 extends MyClass{	// This is public	function Foo2()	{		$this->MyPublic();		$this->MyProtected();		$this->MyPrivate();		// Fatal Error	}}$myclass2 = new MyClass2;$myclass2->MyPublic();	// Works$myclass2->Foo2();		// Public and Protected work, not Private?>

人气教程排行