PHP鐨勫睘鎬

绫荤殑鍙橀噺鎴愬憳鍙仛鈥滃睘鎬р濓紝鎴栬呭彨鈥滃瓧娈碘濄佲滅壒寰佲濓紝鍦ㄦ湰鏂囨。缁熶竴绉颁负鈥滃睘鎬р濄 灞炴у0鏄庢槸鐢卞叧閿瓧public鎴栬卲rotected鎴栬 private寮澶达紝鐒跺悗璺熶竴涓彉閲忔潵缁勬垚銆 灞炴т腑鐨勫彉閲忓彲浠ュ垵濮嬪寲锛屼絾鏄垵濮嬪寲鐨勫煎繀椤绘槸甯告暟锛岃繖閲岀殑甯告暟鏄寚php鑴氭湰鍦ㄧ紪璇戦樁娈垫椂灏变负甯告暟锛岃屼笉鏄湪缂栬瘧闃舵涔嬪悗鍦ㄨ繍琛岄樁娈佃繍绠楀嚭鐨勫父鏁般
Note:

涓轰簡鍏煎php4锛宲hp5澹版槑灞炴т緷鐒跺彲浠ョ洿鎺ヤ娇鐢ㄥ叧閿瓧var锛屾垨鑰呮斁鍦╬ublic, protected, or private涔嬪墠銆備絾鏄痸ar骞朵笉鏄繀椤荤殑銆傚湪php 5.0 鍒5.1.3锛寁ar浼氳涓烘槸搴熷純鐨勶紝 鑰屼笖鎶涘嚭E_STRICT璀﹀憡锛屼絾鏄5.1.3涔嬪悗灏变笉鍐嶈涓烘槸搴熷純锛屼篃涓嶄細鎶涘嚭璀﹀憡銆
濡傛灉鐩存帴浣跨敤var澹版槑灞炴э紝鑰屾病鏈塸ublic, protected, 鎴 private锛宲hp 5 浼氳涓鸿繖涓睘鎬т负public銆

鍦ㄧ被鐨勬垚鍛樻柟娉曢噷闈紝鍙互閫氳繃$this->property(property鏄睘鎬у悕瀛)杩欑鏂瑰紡鏉ヨ闂被鐨勫睘鎬с 鏂规硶锛屼絾鏄璁块棶绫荤殑闈欐佸睘鎬ф垨鑰呭湪闈欐佹柟娉曢噷闈㈠嵈涓嶈兘浣跨敤锛岃屾槸浣跨敤self::$property銆 鏇村Static鍏抽敭瀛楋紝璇锋煡鐪婼tatic 鍏抽敭瀛椼

鍦ㄧ被鐨勯潪闈欐佹柟娉曢噷闈㈠彲浠ヤ娇鐢ㄤ吉鍙橀噺$this锛岃繖涓吉鍙橀噺鏄皟鐢ㄨ鏂规硶鐨勫疄渚嬪寲瀵硅薄寮曠敤锛堜竴鑸潵璇磋瀵硅薄鏄繖涓柟娉曟墍鍦ㄧ被鐨勫疄渚嬪寲瀵硅薄锛 浣嗘槸涔熸湁鍙兘鏄彟澶栫殑绫荤殑瀵硅薄锛屾瘮濡傚湪鍙︿竴涓被閲岄潰浣跨敤闈欐佸寲鏉ヨ皟鐢ㄨ繖涓被鐨勬柟娉 锛夈

Example #1 灞炴у0鏄
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<span style="font-size:14px;"><?php
class SimpleClass
{
   // 閿欒鐨勫睘鎬у0鏄
   public $var1 = 'hello ' . 'world';
   public $var2 = <<<EOD
hello world
EOD;
   public $var3 = 1+2;
   public $var4 = self::myStaticMethod();
   public $var5 = $myVar;
 
   // 姝g‘鐨勫睘鎬у0鏄
   public $var6 = myConstant;
   public $var7 = array(true, false);
 
   //鍦╬hp 5.3.0 鍙婁箣鍚庯紝涓嬮潰鐨勫0鏄庝篃姝g‘
   public $var8 = <<<'EOD'
hello world
EOD;
}
?>
</span>
Note:
鏇村鍏充簬绫/瀵硅薄鐨勫鐞嗗嚱鏁帮紝璇锋煡鐪嬬被/瀵硅薄 鍑芥暟銆

璺焗eredocs涓嶅悓, nowdocs鑳藉浣跨敤鍦ㄩ潤鎬佸彉閲忥紝涔熻兘浣跨敤鍦ㄩ潤鎬佸0鏄庛

Example #2 绀轰緥锛氫娇鐢╪owdoc鍒濆鍖栧睘鎬
1
2
3
4
5
6
7
8
<span style="font-size:14px;"><?php
class foo {
   // As of PHP 5.3.0
   public $bar = <<<'EOT'
bar
EOT;
}
?></span>

Note:

鍦╬hp 5.3.0 澧炲姞浜哊owdoc鐨勬敮鎸併


浠ヤ笂灏辨槸PHP鐨勫睘鎬х殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒



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