PHP基礎(chǔ)之類和對(duì)象6——訪問控制:public/protected/private
對(duì)屬性或方法的訪問控制,是通過在前面添加關(guān)鍵字 public(公有),protected(受保護(hù))或 private(私有)來實(shí)現(xiàn)的。被定義為公有的類成員可以在任何地方被訪問。被定義為受保護(hù)的類成員則可以被其自身以及其子類和父類訪問。被定義為私有的類成員則只能被其定義所在的類訪問。
一、屬性的訪問控制類屬性必須定義為公有,受保護(hù),私有之一。如果用?var?定義,則被視為公有。Example #1 屬性聲明
class MyClass{ public $public = ’Public’; protected $protected = ’Protected’; private $private = ’Private’; function printHello() {echo $this->public.’<br>’;echo $this->protected.’<br>’;echo $this->private.’<br>’; }}$obj = new MyClass();echo $obj->public; //這行可以正常運(yùn)行echo $obj->protected; //這行會(huì)產(chǎn)生一個(gè)致命錯(cuò)誤echo $obj->private; //這行也會(huì)產(chǎn)生一個(gè)致命錯(cuò)誤$obj->printHello(); //正常輸出public、protected、private的值class MyClass2 extends MyClass{ protected $protected = ’Protected2’; function printHello(){echo $this->public;echo $this->protected;echo $this->private; }}$obj2 = new MyClass2();echo $obj2->public; //這行可以正常執(zhí)行echo $obj2->private; //未定義privateecho $obj2->protected; //產(chǎn)生一個(gè)致命的錯(cuò)誤$obj2->printHello(); //輸出Public、Protected和Undefined
Note: 為了兼容性考慮,在 PHP 4 中使用?var?關(guān)鍵字對(duì)變量進(jìn)行定義的方法在 PHP 5 中仍然有效(只是作為 public 關(guān)鍵字的一個(gè)別名)。在 PHP 5.1.3 之前的版本,該語(yǔ)法會(huì)產(chǎn)生一個(gè)?E_STRICT?警告。
方法的訪問控制類中的方法可以被定義為公有,私有或受保護(hù)。如果沒有設(shè)置這些關(guān)鍵字,則該方法默認(rèn)為公有。Example #2 方法聲明
class MyClass{ //聲明一個(gè)公有的構(gòu)造函數(shù) public function __construct(){} //聲明一個(gè)公有的方法 public function MyPublic(){} //聲明一個(gè)受保護(hù)的方法 protected function MyProtected(){} //聲明一個(gè)私有的方法 private function MyPrivate(){} //此方法為公有 function Foo() {$this->MyPublic();$this->MyProtected();$this->MyPrivate(); }}$myclass = new MyClass;$myclass -> MyPublic(); //這行能被正常執(zhí)行$myclass -> MyProtected(); //這行會(huì)產(chǎn)生一個(gè)致命錯(cuò)誤$myclass -> MyPrivate(); //這行會(huì)產(chǎn)生一個(gè)致命錯(cuò)誤$myclass -> Foo(); //公有,受保護(hù),私有都可以執(zhí)行class MyClass2 extends MyClass{ //此方法為公有 function Foo2() {$this->MyPublic();$this->MyProtected();$this->MyPrivate(); //這行會(huì)產(chǎn)生一個(gè)致命錯(cuò)誤 }}$myclass2 = new MyClass2;$myclass2 -> MyPublic(); //這行能被正常執(zhí)行$myclass2 -> Foo2(); //公有和受保護(hù)的都可以執(zhí)行,但私有的不行class Bar{ public function test(){$this->testPrivate();$this -> testPublic(); } public function testPublic(){echo 'Bar::testPublic<br>'; } private function testPrivate(){ echo 'Bar::testPrivate<br>'; }}class Foo extends Bar{ public function testPublic(){echo 'Foo::testPublic<br>'; } public function testPrivate(){ echo 'Foo::testPrivate<br>'; }}$myFoo = new Foo();$myFoo -> test(); //Bar::testPrivate//Foo::testPublic其它對(duì)象的訪問控制
同一個(gè)類的對(duì)象即使不是同一個(gè)實(shí)例也可以互相訪問對(duì)方的私有與受保護(hù)成員。這是由于在這些對(duì)象的內(nèi)部具體實(shí)現(xiàn)的細(xì)節(jié)都是已知的。Example #3 訪問同一個(gè)對(duì)象類型的私有成員
class Test{ private $foo; public function __construct($foo) {$this->foo = $foo; } private function bar() {echo 'Accessed the private method.'; } public function baz(Test $other) {//我們可以在這里改變私有屬性的值$other->foo = ’hello’;var_dump($this->foo);//我們也可以在這里調(diào)用私有的方法$other->bar(); }}$test = new Test(’test’);$test -> baz(new Test(’other’));
輸出結(jié)果:
string(4) 'test'Accessed the private method.
相關(guān)文章:
1. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除2. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)3. 爬取今日頭條Ajax請(qǐng)求4. JavaWeb Servlet中url-pattern的使用5. ASP基礎(chǔ)知識(shí)VBScript基本元素講解6. 如何使用瀏覽器擴(kuò)展篡改網(wǎng)頁(yè)中的JS 文件7. jsp EL表達(dá)式詳解8. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)9. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼10. jsp中sitemesh修改tagRule技術(shù)分享
