Home Backend Development PHP Tutorial php面向对象(OOP 编程)- 类访问修饰符

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

Jun 20, 2016 pm 12:30 PM

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


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

<?php/** * 类属性访问控制 * Define MyClass */class MyClass{	public $public = 'Public';	protected $protected = 'Protected';	private $private = 'Private';	function printHello()	{		echo $this->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?>
Copy after login



<?php/** * 类方法访问控制 * Define MyClass */class MyClass{	// Contructors must be public	public function __construct() { }	// Declare a public method	public function MyPublic() { }	// Declare a protected method	protected function MyProtected() { }       //abstract protected function funProtect($param);抽象方法只能在抽象类中定义	// Declare a private method	private function MyPrivate() { }    	// This is public	function Foo()	{		$this->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?>
Copy after login




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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles