PHP鐨勭被甯搁噺
甯搁噺鐨勫煎繀椤绘槸涓涓畾鍊硷紝涓嶈兘鏄彉閲忥紝绫诲睘鎬ф垨鍏跺畠鎿嶄綔锛堝鍑芥暟璋冪敤锛夌殑缁撴灉銆
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浜戝叾瀹冪浉鍏虫枃绔狅紒