PHP璁块棶鎺у埗

瀵瑰睘鎬ф垨鏂规硶鐨勮闂帶鍒讹紝鏄氳繃鍦ㄥ墠闈㈡坊鍔犲叧閿瓧 public銆乸rotected 鎴 private 鏉ュ疄鐜扮殑銆傜敱 public 鎵瀹氫箟鐨勭被鎴愬憳鍙互鍦ㄤ换浣曞湴鏂硅璁块棶锛涚敱 protected 鎵瀹氫箟鐨勭被鎴愬憳鍒欏彲浠ヨ鍏舵墍鍦ㄧ被鐨勫瓙绫诲拰鐖剁被璁块棶锛堝綋鐒讹紝璇ユ垚鍛樻墍鍦ㄧ殑绫讳篃鍙互璁块棶锛夛紱鑰岀敱 private 瀹氫箟鐨勭被鎴愬憳鍒欏彧鑳借鍏舵墍鍦ㄧ被璁块棶銆

瀵瑰睘鎬ф垨鏂规硶鐨勮闂帶鍒讹紝鏄氳繃鍦ㄥ墠闈㈡坊鍔犲叧閿瓧 public銆乸rotected 鎴 private 鏉ュ疄鐜扮殑銆傜敱 public 鎵瀹氫箟鐨勭被鎴愬憳鍙互鍦ㄤ换浣曞湴鏂硅璁块棶锛涚敱 protected 鎵瀹氫箟鐨勭被鎴愬憳鍒欏彲浠ヨ鍏舵墍鍦ㄧ被鐨勫瓙绫诲拰鐖剁被璁块棶锛堝綋鐒讹紝璇ユ垚鍛樻墍鍦ㄧ殑绫讳篃鍙互璁块棶锛夛紱鑰岀敱 private 瀹氫箟鐨勭被鎴愬憳鍒欏彧鑳借鍏舵墍鍦ㄧ被璁块棶銆

瀵圭被鎴愬憳鐨勮闂帶鍒

绫绘垚鍛橀兘蹇呴』浣跨敤鍏抽敭瀛梡ublic銆乸rotected 鎴 private 杩涜瀹氫箟

Example #1 澹版槑绫绘垚鍛

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<code><?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; // 杩欒鑳借姝e父鎵ц
echo $obj->protected; // 杩欒浼氫骇鐢熶竴涓嚧鍛介敊璇
echo $obj->private; // 杩欒涔熶細浜х敓涓涓嚧鍛介敊璇
$obj->printHello(); // 杈撳嚭 Public銆丳rotected 鍜 Private
 
/**
 * Define MyClass2
 */
class MyClass2 extends MyClass
{
    // 鍙互瀵 public 鍜 protected 杩涜閲嶅畾涔夛紝浣 private 鑰屼笉鑳
    protected $protected = 'Protected2';
 
    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
 
$obj2 = new MyClass2();
echo $obj->public; // 杩欒鑳借姝e父鎵ц
echo $obj2->private; // 鏈畾涔 private
echo $obj2->protected; // 杩欒浼氫骇鐢熶竴涓嚧鍛介敊璇
$obj2->printHello(); // 杈撳嚭 Public銆丳rotected2锛屼絾涓嶄細杈撳嚭 Private
 
class Bar
{
    public function test() {
        $this->testPrivate();
        $this->testPublic();
    }
 
    public function testPublic() {
        echo "Bar::testPublic ";
    }
     
    private function testPrivate() {
        echo "Bar::testPrivate ";
    }
}
 
class Foo extends Bar
{
    public function testPublic() {
        echo "Foo::testPublic ";
    }
     
    private function testPrivate() {
        echo "Foo::testPrivate ";
    }
}
 
$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate
                // Foo::testPublic
?></code>

 

Note: 涓轰簡鍏煎鎬ц冭檻锛屽湪 PHP 4 涓娇鐢 var 鍏抽敭瀛楀鍙橀噺杩涜瀹氫箟鐨勬柟娉曞湪 PHP 5 涓粛鐒舵湁鏁堬紙鍙槸浣滀负 public 鍏抽敭瀛楃殑涓涓埆鍚嶏級銆傚湪 PHP 5.1.3 涔嬪墠鐨勭増鏈紝璇ヨ娉曚細浜х敓涓涓 E_STRICT 璀﹀憡銆

瀵规柟娉曠殑璁块棶鎺у埗

绫讳腑鐨勬柟娉曢兘蹇呴』浣跨敤鍏抽敭瀛梡ublic銆乸rotected 鎴 private 杩涜瀹氫箟銆傚鏋滄病鏈夎缃繖浜涘叧閿瓧锛屽垯璇ユ柟娉曚細琚缃垚榛樿鐨 public銆

Example #2 澹版槑绫讳腑鐨勬柟娉

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<code><?php
/**
 * Define MyClass
 */
class MyClass
{
    // 鏋勯犲嚱鏁板繀椤绘槸 public
    public function __construct() { }
 
    // 澹版槑涓涓 public 鐨勬柟娉
    public function MyPublic() { }
 
    // 澹版槑涓涓 protected 鐨勬柟娉
    protected function MyProtected() { }
 
    // 澹版槑涓涓 private 鐨勬柟娉
    private function MyPrivate() { }
 
    // 杩欎釜鏂规硶涔熸槸 public 鐨
    function Foo()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate();
    }
}
 
$myclass = new MyClass;
$myclass->MyPublic(); // 杩欒鑳借姝e父鎵ц
$myclass->MyProtected(); // 杩欒浼氫骇鐢熶竴涓嚧鍛介敊璇
$myclass->MyPrivate(); // 杩欒浼氫骇鐢熶竴涓嚧鍛介敊璇
$myclass->Foo(); // Public銆丳rotected 鍜 Private 閮借璋冪敤浜
 
 
/**
 * Define MyClass2
 */
class MyClass2 extends MyClass
{
    // This is public
    function Foo2()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate(); // 杩欒浼氫骇鐢熶竴涓嚧鍛介敊璇
    }
}
 
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // 杩欒鑳借姝e父鎵ц
$myclass2->Foo2(); // Public 鍜 Protected 閮借璋冪敤浜嗭紝浣 Private 涓嶄細琚皟鐢
?></code>

浠ヤ笂灏辨槸PHP璁块棶鎺у埗鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=101