<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>潘锦的空间 &#187; 常量</title>
	<atom:link href="https://www.phppan.com/tag/%e5%b8%b8%e9%87%8f/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sat, 25 Apr 2026 00:56:17 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>几个 PHP 的“魔术常量”</title>
		<link>https://www.phppan.com/2012/05/php-magic-const-var/</link>
		<comments>https://www.phppan.com/2012/05/php-magic-const-var/#comments</comments>
		<pubDate>Mon, 28 May 2012 00:43:44 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP源码]]></category>
		<category><![CDATA[PHP源码阅读笔记]]></category>
		<category><![CDATA[常量]]></category>
		<category><![CDATA[魔术常量]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1689</guid>
		<description><![CDATA[PHP向它运行的任何脚本提供了大量的预定义常量。 不过很多常量都是由不同的扩展库定义的，只有在加载了这些扩展库 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP向它运行的任何脚本提供了大量的预定义常量。<br />
不过很多常量都是由不同的扩展库定义的，只有在加载了这些扩展库时才会出现，或者动态加载后，或者在编译时已经包括进去了。</p>
<p>有七个魔术常量它们的值随着它们在代码中的位置改变而改变。例如 __LINE__ 的值就依赖于它在脚本中所处的行来决定。这些特殊的常量不区分大小写。在<a href="http://cn2.php.net/manual/zh/language.constants.predefined.php">手册</a>中这几个变量的简单说明如下：<br />
几个 PHP 的“魔术常量”</p>
<table>
<tr>
<th>名称</th>
<th>说明</th>
</tr>
<tr>
<td>__LINE__</td>
<td>文件中的当前行号。</td>
</tr>
<tr>
<td>__FILE__</td>
<td>文件的完整路径和文件名。如果用在被包含文件中，则返回被包含的文件名。自 PHP 4.0.2 起，__FILE__ 总是包含一个绝对路径（如果是符号连接，则是解析后的绝对路径），而在此之前的版本有时会包含一个相对路径。</td>
</tr>
<tr>
<td>__DIR__</td>
<td>文件所在的目录。如果用在被包括文件中，则返回被包括的文件所在的目录。它等价于 dirname(__FILE__)。除非是根目录，否则目录中名不包括末尾的斜杠。（PHP 5.3.0中新增） =</td>
</tr>
<tr>
<td>__FUNCTION__</td>
<td>函数名称（PHP 4.3.0 新加）。自 PHP 5 起本常量返回该函数被定义时的名字（区分大小写）。在 PHP 4 中该值总是小写字母的。</td>
</tr>
<tr>
<td>__CLASS__</td>
<td>类的名称（PHP 4.3.0 新加）。自 PHP 5 起本常量返回该类被定义时的名字（区分大小写）。在 PHP 4 中该值总是小写字母的。</td>
</tr>
<tr>
<td>__METHOD__</td>
<td>类的方法名（PHP 5.0.0 新加）。返回该方法被定义时的名字（区分大小写）。</td>
</tr>
<tr>
<td>__NAMESPACE__</td>
<td>当前命名空间的名称（大小写敏感）。这个常量是在编译时定义的（PHP 5.3.0 新增）</td>
</tr>
<tr>
</table>
<p>在写这篇文章时，由于把思维局限在语法解析和运行时赋值，导致寻找了好久都没有发现实现原因。还是<a href="http://www.csie.nctu.edu.tw/~chlo/web/docs/doc/data/php/12.htm">网上的一篇文章</a>将我们的思维找回到词法分析。PHP内核会在词法解析时将这些相对静态的内容赋值给这些变量，而不是在运行时执行的分析。如下PHP代码：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?PHP</span>
<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__LINE__</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> demo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
demo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>其实PHP已经在词法解析时将这些常量换成了对应的值，以上的代码可以看成如下的PHP代码：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?PHP</span>
<span style="color: #b1b100;">echo</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> demo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;demo&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
demo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>如果我们使用VLD扩展查看以上的两段代码生成的中间代码，你会发现其结果是一样的。</p>
<p>前面我们有说PHP是在词法分析时做的赋值替换操作，以__FUNCTION__为例，在Zend/zend_language_scanner.l文件中，__FUNCTION__是一个需要分析的元标记（token）：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>ST_IN_SCRIPTING<span style="color: #339933;">&gt;</span><span style="color: #ff0000;">&quot;__FUNCTION__&quot;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">char</span> <span style="color: #339933;">*</span>func_name <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>CG<span style="color: #009900;">&#40;</span>active_op_array<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		func_name <span style="color: #339933;">=</span> CG<span style="color: #009900;">&#40;</span>active_op_array<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>function_name<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>func_name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		func_name <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	zendlval<span style="color: #339933;">-&gt;</span>value.<span style="color: #202020;">str</span>.<span style="color: #202020;">len</span> <span style="color: #339933;">=</span> <span style="color: #000066;">strlen</span><span style="color: #009900;">&#40;</span>func_name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	zendlval<span style="color: #339933;">-&gt;</span>value.<span style="color: #202020;">str</span>.<span style="color: #202020;">val</span> <span style="color: #339933;">=</span> estrndup<span style="color: #009900;">&#40;</span>func_name<span style="color: #339933;">,</span> zendlval<span style="color: #339933;">-&gt;</span>value.<span style="color: #202020;">str</span>.<span style="color: #202020;">len</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	zendlval<span style="color: #339933;">-&gt;</span>type <span style="color: #339933;">=</span> IS_STRING<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> T_FUNC_C<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>就是这里，当当前中间代码处于一个函数中时，则将当前函数名赋值给zendlval，如果没有，则将空字符串赋值给zendlval(因此在顶级作用域名中直接打印__FUNCTION__会输出空格)。这个值在语法解析时会直接赋值给返回值。这样我们就在生成的中间代码中看到了这些常量的位置都已经赋值好了。</p>
<p>和__FUNCTION__类似，在其附近的位置，上面表格中的其它常量也进行了类似的操作。在PHP5.4中增加了对于trait类的常量定义：__TRAIT__。</p>
<p><strong>这些常量其实相当于一个常量模板，或者说是一个占位符，在词法解析时这些模板或占位符就被替换成实际的值 </strong></p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2012/05/php-magic-const-var/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
