PHP閲嶈浇

閲嶈浇,PHP鎵鎻愪緵鐨"閲嶈浇"锛坥verloading锛夋槸鎸囧姩鎬佸湴"鍒涘缓"绫诲睘鎬у拰鏂规硶銆傛垜浠槸閫氳繃 榄旀湳鏂规硶锛坢agic methods锛夋潵瀹炵幇鐨勩

PHP鎵鎻愪緵鐨"閲嶈浇"锛坥verloading锛夋槸鎸囧姩鎬佸湴"鍒涘缓"绫诲睘鎬у拰鏂规硶銆傛垜浠槸閫氳繃 榄旀湳鏂规硶锛坢agic methods锛夋潵瀹炵幇鐨勩
褰撹皟鐢ㄥ綋鍓嶇幆澧冧笅鏈畾涔夋垨涓嶅彲瑙佺殑绫诲睘鎬ф垨鏂规硶鏃讹紝閲嶈浇鏂规硶浼氳璋冪敤銆傛湰鑺傚悗闈㈠皢浣跨敤 "涓嶅彲璁块棶鎴愬憳锛坕naccessible members锛"鍜"涓嶅彲璁块棶鏂规硶锛坕naccessible methods锛"鏉ョО鍛艰繖浜涙湭瀹氫箟鎴栦笉鍙鐨勭被灞炴ф垨鏂规硶銆

鎵鏈夌殑閲嶈浇鏂规硶閮藉繀椤昏澹版槑涓簆ublic銆

Note:

杩欎簺榄旀湳鏂规硶鐨勫弬鏁伴兘涓嶈兘 閫氳繃寮曠敤浼犻掋

Note:

PHP涓殑"閲嶈浇"涓庡叾瀹冪粷澶у鏁伴潰鍚戝璞¤瑷涓嶅悓銆備紶缁熺殑"閲嶈浇"鏄敤浜庢彁渚涘涓悓鍚嶇殑 绫绘柟娉曪紝浣嗗悇鏂规硶鐨勫弬鏁扮被鍨嬪拰涓暟涓嶅悓銆

鏇存柊鏃ュ織
鐗堟湰    璇存槑
5.1.0    鏂板 __isset() 鍜 __unset()涓や釜榄旀湳鏂规硶.
5.3.0    鏂板 __callStatic()榄旀湳鏂规硶. 鍙鎬ф湭璁剧疆涓簆ublic鎴栨湭澹版槑涓簊tatic鐨勬椂鍊欎細浜х敓涓涓鍛
灞炴ч噸杞
void __set ( string $name , mixed $value ) mixed __get ( string $name ) bool __isset ( string $name ) void __unset ( string $name )
鍦ㄧ粰鏈畾涔夌殑鍙橀噺璧嬪兼椂锛宊_set() 浼氳璋冪敤銆

璇诲彇鏈畾涔夌殑鍙橀噺鐨勫兼椂锛宊_get() 浼氳璋冪敤銆

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
<code><?php
class MemberTest {
      /**  琚噸杞界殑鏁版嵁淇濆瓨鍦ㄦ  */
    private $data = array();
 
  
    /**  閲嶈浇涓嶈兘琚敤鍦ㄥ凡缁忓畾涔夌殑灞炴  */
    public $declared = 1;
 
       /**  鍙湁浠庣被澶栭儴璁块棶杩欎釜灞炴ф椂锛岄噸杞芥墠浼氬彂鐢 */
    private $hidden = 2;
 
    public function __set($name, $value) {
        echo "Setting '$name' to '$value' ";
        $this->data[$name] = $value;
    }
 
    public function __get($name) {
        echo "Getting '$name' ";
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }
 
        $trace = debug_backtrace();
        trigger_error(
            'Undefined property via __get(): ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        return null;
    }
 
    /**  PHP 5.1.0涔嬪悗鐗堟湰 */
    public function __isset($name) {
        echo "Is '$name' set? ";
        return isset($this->data[$name]);
    }
 
    /**  PHP 5.1.0涔嬪悗鐗堟湰 */
    public function __unset($name) {
        echo "Unsetting '$name' ";
        unset($this->data[$name]);
    }
 
    /**  闈為瓟鏈柟娉  */
    public function getHidden() {
        return $this->hidden;
    }
}
 
 
echo "<pre> ";
 
$obj = new MemberTest;
 
$obj->a = 1;
echo $obj->a . " ";
 
var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo " ";
 
echo $obj->declared . " ";
 
echo "Let's experiment with the private property named 'hidden': ";
echo "Privates are visible inside the class, so __get() not used... ";
echo $obj->getHidden() . " ";
echo "Privates not visible outside of class, so __get() is used... ";
echo $obj->hidden . " ";
?></code>

浠ヤ笂渚嬬▼浼氳緭鍑猴細

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Setting 'a' to '1'
Getting 'a'
1
 
Is 'a' set?
bool(true)
Unsetting 'a'
Is 'a' set?
bool(false)
 
1
 
Let's experiment with the private property named 'hidden':
Privates are visible inside the class, so __get() not used...
2
Privates not visible outside of class, so __get() is used...
Getting 'hidden'
 
 
Notice:  Undefined property via __get(): hidden in <file> on line 70 in <file> on line 29

鏂规硶閲嶈浇

mixed __call ( string $name , array $arguments ) mixed __callStatic ( string $name , array $arguments )
褰撹皟鐢ㄤ竴涓笉鍙闂柟娉曪紙濡傛湭瀹氫箟锛屾垨鑰呬笉鍙锛夋椂锛宊_call() 浼氳璋冪敤銆

褰撳湪闈欐佹柟娉曚腑璋冪敤涓涓笉鍙闂柟娉曪紙濡傛湭瀹氫箟锛屾垨鑰呬笉鍙锛夋椂锛宊_callStatic() 浼氳璋冪敤銆

$name鍙傛暟鏄璋冪敤鐨勬柟娉曞悕绉般$arguments鍙傛暟鏄竴涓暟缁勶紝鍖呭惈鐫瑕佷紶閫掔粰鏂规硶鐨勫弬鏁般

Example #2 浣跨敤 __call 鍜 ___callStatic 璋冪敤瀵硅薄涓殑鏂规硶
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<code><?php
class MethodTest {
    public function __call($name, $arguments) {
        // 娉ㄦ剰: $name 鍖哄垎澶у皬鍐
        echo "Calling object method '$name' "
             . implode(', ', $arguments). " ";
    }
 
    /**  PHP 5.3.0涔嬪悗鐗堟湰  */
    public static function __callStatic($name, $arguments) {
        // Note: value of $name is case sensitive.
        echo "Calling static method '$name' "
             . implode(', ', $arguments). " ";
    }
}
 
$obj = new MethodTest;
$obj->runTest('in object context');
 
MethodTest::runTest('in static context');  // PHP 5.3.0涔嬪悗鐗堟湰
?></code>

浠ヤ笂渚嬬▼浼氳緭鍑猴細

1
2
Calling object method 'runTest' in object context
Calling static method 'runTest' in static context
 



褰撳鏈畾涔夌殑鍙橀噺璋冪敤isset() 鎴 empty()鏃讹紝__isset() 浼氳璋冪敤銆

褰撳鏈畾涔夌殑鍙橀噺璋冪敤unset()鏃讹紝__unset() 浼氳璋冪敤銆

鍙傛暟$name鏄寚瑕佹搷浣滅殑鍙橀噺鍚嶇О銆俖_set() 鏂规硶鐨$value 鍙傛暟鎸囧畾浜$name鍙橀噺鐨勫笺

灞炴ч噸杞藉彧鑳藉湪瀵硅薄涓繘琛屻傚湪闈欐佹柟娉曚腑锛岃繖浜涢瓟鏈柟娉曞皢涓嶄細琚皟鐢ㄣ傛墍浠ヨ繖浜涙柟娉曢兘涓嶈兘琚 澹版槑涓簊tatic銆

Example #1 浣跨敤__get, __set, __isset 鍜 __unset 杩涜閲嶈浇


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



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