PHP鐨勭被甯搁噺

鍐呭鎽樿
鎴戜滑鍙互鍦ㄧ被涓畾涔夊父閲忋傚父閲忕殑鍊煎皢濮嬬粓淇濇寔涓嶅彉銆傚湪瀹氫箟鍜屼娇鐢ㄥ父閲忕殑鏃跺欎笉闇瑕佷娇鐢$绗﹀彿銆傚父閲忕殑鍊煎繀椤绘槸涓涓畾鍊硷紝涓嶈兘鏄彉閲忥紝绫诲睘鎬ф垨鍏跺畠鎿嶄綔锛堝鍑芥暟璋冪敤锛夌殑缁撴灉銆
鏂囩珷姝f枃
鎴戜滑鍙互鍦ㄧ被涓畾涔夊父閲忋傚父閲忕殑鍊煎皢濮嬬粓淇濇寔涓嶅彉銆傚湪瀹氫箟鍜屼娇鐢ㄥ父閲忕殑鏃跺欎笉闇瑕佷娇鐢$绗﹀彿銆

甯搁噺鐨勫煎繀椤绘槸涓涓畾鍊硷紝涓嶈兘鏄彉閲忥紝绫诲睘鎬ф垨鍏跺畠鎿嶄綔锛堝鍑芥暟璋冪敤锛夌殑缁撴灉銆

Its also possible for interfaces to have constants. Look at the interface documentation for examples. 鎺ュ彛锛坕nterface锛変腑涔熷彲浠ュ畾涔夊父閲忋傝鏌ョ湅鎺ュ彛鐨勬枃妗h幏寰楁洿澶氱ず渚嬨

PHP5.3.0涔嬪悗锛屾垜浠彲浠ョ敤涓涓彉閲忔潵鍔ㄦ佽皟鐢ㄧ被銆備絾璇ュ彉閲忕殑鍊间笉鑳戒负鍏抽敭瀛梥elf, parent 鎴杝tatic銆

Example #1 瀹氫箟鍜屼娇鐢ㄤ竴涓被甯搁噺
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<span style="font-size:14px;"><?php
class MyClass
{
    const constant = 'constant value';
 
    function showConstant() {
        echo  self::constant . " ";
    }
}
 
echo MyClass::constant . " ";
 
$classname = "MyClass";
echo $classname::constant . " "; // PHP 5.3.0涔嬪悗
 
$class = new MyClass();
$class->showConstant();
 
echo $class::constant." "; // PHP 5.3.0涔嬪悗
?></span>

Example #2 闈欐佹暟鎹ず渚
1
2
3
4
5
6
7
8
<span style="font-size:14px;"><?php
class foo {
    // PHP 5.3.0涔嬪悗
    const bar = <<<'EOT'
bar
EOT;
}
?></span>

鍜宧eredocs锛堝瓧绗︿覆杈圭晫绗︼級涓嶅悓锛宯owdocs鍙互鐢ㄥ湪浠讳綍闈欐佹暟鎹腑銆
 

Note:

PHP 5.3.0璧稰HP鏀寔Nowdoc鍔熻兘銆


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



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