__METHOD__ 是PHP5之后新增的魔术常量,表示的是类文法的名称。魔术常量是一种PHP预定义常量,它的值可以是变化的,PHP中的其它已经存在的魔术常量有__LINE__、__FILE__、__FUNCTION__、__CLASS__等。

下面就用一小段代码来演示一下__METHOD__的用法。

1
2
3
4
5
6
7
8
9
10
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> chhua<span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//转载注明www.chhua.com</span>
	<span style="color: #000000;font-weight: bold">function</span> test<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">echo</span> <span style="color: #009900;font-weight: bold">__METHOD__</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000088">$e</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> chhua<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
<span style="color: #000088">$e</span><span style="color: #339933">-></span><span style="color: #004000">test</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出:chhua::test</span>