久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

PHP基礎(chǔ)之類(lèi)和對(duì)象6——訪(fǎng)問(wèn)控制:public/protected/private

瀏覽:105日期:2022-09-14 17:27:44

對(duì)屬性或方法的訪(fǎng)問(wèn)控制,是通過(guò)在前面添加關(guān)鍵字 public(公有),protected(受保護(hù))或 private(私有)來(lái)實(shí)現(xiàn)的。被定義為公有的類(lèi)成員可以在任何地方被訪(fǎng)問(wèn)。被定義為受保護(hù)的類(lèi)成員則可以被其自身以及其子類(lèi)和父類(lèi)訪(fǎng)問(wèn)。被定義為私有的類(lèi)成員則只能被其定義所在的類(lèi)訪(fǎng)問(wèn)。

一、屬性的訪(fǎng)問(wèn)控制

類(lèi)屬性必須定義為公有,受保護(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?警告。

方法的訪(fǎng)問(wèn)控制

類(lèi)中的方法可以被定義為公有,私有或受保護(hù)。如果沒(méi)有設(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ì)象的訪(fǎng)問(wèn)控制

同一個(gè)類(lèi)的對(duì)象即使不是同一個(gè)實(shí)例也可以互相訪(fǎng)問(wèn)對(duì)方的私有與受保護(hù)成員。這是由于在這些對(duì)象的內(nèi)部具體實(shí)現(xiàn)的細(xì)節(jié)都是已知的。Example #3 訪(fǎng)問(wèn)同一個(gè)對(duì)象類(lèi)型的私有成員

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.

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 沅陵县| 营口市| 南溪县| 长白| 东兰县| 临西县| 明水县| 巢湖市| 利辛县| 六盘水市| 大城县| 无极县| 大竹县| 威远县| 宜兴市| 当雄县| 启东市| 荆门市| 浑源县| 南汇区| 西安市| 吉林省| 康乐县| 介休市| 松原市| 滦平县| 高碑店市| 阿克| 宝山区| 吴忠市| 辛集市| 怀安县| 古蔺县| 营山县| 安庆市| 海门市| 德格县| 太康县| 凌海市| 兴隆县| 滁州市|