<?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>haohtml&#039;s blog &#187; 命名空间</title>
	<atom:link href="http://blog.haohtml.com/tag/%e5%91%bd%e5%90%8d%e7%a9%ba%e9%97%b4/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.haohtml.com</link>
	<description>haohtml&#039;s life and works</description>
	<lastBuildDate>Tue, 15 May 2012 09:00:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>PHP命名空间的分隔符是“\”</title>
		<link>http://blog.haohtml.com/archives/604</link>
		<comments>http://blog.haohtml.com/archives/604#comments</comments>
		<pubDate>Thu, 13 Nov 2008 00:58:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[js框架]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[命名空间]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=604</guid>
		<description><![CDATA[PHP最终开始支持namespace（命名空间）。 但是经过开发者几个小时的讨论后，他们决定采用 “\” 作为分隔符，而不是更流行的“ :: ”。Fredrik Holmström指出了这种分隔方法可能会产生的问题，如打字出错(但不会出现错误提示)，IDE兼容性问题，字符数字等等，好处则是容易输入和分析。"<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="PHP的命名空间" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP的命名空间</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="第十四节--命名空间 -- Classes and Objects in PHP5 [14]" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">第十四节--命名空间 -- Classes and Objects in PHP5 [14]</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="50个非常有用的PHP工具" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F7059&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/02/20541028.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">50个非常有用的PHP工具</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="404错误页面重定向 PHP使用HEADER头发送状态(Status)信息(一)" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3070&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4028468.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">404错误页面重定向 PHP使用HEADER头发送状态(Status)信息(一)</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>PHP最终开始支持namespace（命名空间）。<em> 但是经过开发者<a href="http://wiki.php.net/_media/rfc/php.ns.txt?id=rfc%3Anamespaceseparator&amp;cache=cache">几个小时的讨论后</a>，他们决定<a href="http://news.php.net/php.internals/41374">采用 “\” </a>作为分隔符，而不是更流行的“ :: ”。<a href="http://loveandtheft.org/2008/10/26/set-sail-for-fail-php-namespaces/">Fredrik Holmström</a>指出了这种分隔方法可能会产生的问题，如打字出错(但不会出现错误提示)，IDE兼容性问题，字符数字等等，<a href="http://wiki.php.net/rfc/namespaceseparator">好处</a>则是容易输入和分析。"</em></p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="PHP的命名空间" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP的命名空间</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="第十四节--命名空间 -- Classes and Objects in PHP5 [14]" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">第十四节--命名空间 -- Classes and Objects in PHP5 [14]</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="50个非常有用的PHP工具" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F7059&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/02/20541028.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">50个非常有用的PHP工具</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="404错误页面重定向 PHP使用HEADER头发送状态(Status)信息(一)" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3070&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4028468.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">404错误页面重定向 PHP使用HEADER头发送状态(Status)信息(一)</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/604/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP的命名空间</title>
		<link>http://blog.haohtml.com/archives/599</link>
		<comments>http://blog.haohtml.com/archives/599#comments</comments>
		<pubDate>Thu, 13 Nov 2008 00:53:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[命名空间]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=599</guid>
		<description><![CDATA[命名变量,函数和类是挺困难的,除了要考虑到变量的名称要易于理解,还要担心是否这个名称在其它某个地方已经使用过. 在一小段脚本中,第二个问题是基本问题. 当你考虑重用你的代码, 在这之后的项目代码必须避免使用你用过的命名. 通常来说,可重用的代码总是包含在函数或类中, 需要处理许多可能发生的命名冲突. 但函数和类之间也可能发生命名冲突. 你可以尝试避免出现这种情况,通过在所有类前添加前缀的方法,或者你可以使用namespace语句.

Namespace关键字给一块代码命名. 在这个代码块外部,脚本必须用操作符::加上命名空间的名称来引用这个代码块. 引用静态的类成员也是用相同的方法. 在命名空间内代码不需要声明命名空间, 它本身就是默认的. 这种方法比添加前缀的方法好. 你的代码可由此变得更加紧凑和可读.
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="第十四节--命名空间 -- Classes and Objects in PHP5 [14]" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">第十四节--命名空间 -- Classes and Objects in PHP5 [14]</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="PHP命名空间的分隔符是“\”" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP命名空间的分隔符是“\”</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="从php核心代码看require和include的区别" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4311&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4027588.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">从php核心代码看require和include的区别</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="PHP验证码：随机扭曲、旋转、变形（仿Google验证码）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4354&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4027581.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP验证码：随机扭曲、旋转、变形（仿Google验证码）</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<div class="m_l_cont_note1">
<ul>
<li><span class="fa"><span style="color: #cc0000;">摘要：</span></span>命名变量,函数和类是挺困难的,除了要考虑到变量的名称要易于理解,还要担心是否这个名称在其它某个地方已经使用过. 在一小段脚本中,第二个问题是基本问题. 当你考虑重用你的代码, 在这之后的项目代码必须避免使用你用过的命名. 通常来说,可重用的代码总是包含在函数或类中, 需要处理许多可能发生的命名冲突. 但函数和类之间也可能发生命名冲突. 你可以尝试避免出现这种情况,通过在所有类前添加前缀的方法,或者你可以使用namespace语句.</li>
<li><span class="fa"><span style="color: #cc0000;">标签：</span></span><a href="http://www.51cto.com/php/search.php?keyword=PHP%C3%FC%C3%FB%BF%D5%BC%E4" target="_blank"><span style="color: #0e60ac;">PHP命名空间</span></a>  <a href="http://www.51cto.com/php/search.php?keyword=PHP" target="_blank"><span style="color: #0e60ac;">PHP</span></a></li>
</ul>
</div>
<div class="m_l_cont_text">
<p> </p>
<p>命名变量,函数和类是挺困难的,除了要考虑到变量的名称要易于理解,还要担心是否这个名称在其它某个地方已经使用过. 在一小段脚本中,第二个问题是基本问题. 当你考虑重用你的代码, 在这之后的项目代码必须避免使用你用过的命名. 通常来说,可重用的代码总是包含在函数或类中, 需要处理许多可能发生的命名冲突. 但函数和类之间也可能发生命名冲突. 你可以尝试避免出现这种情况,通过在所有类前添加前缀的方法,或者你可以使用namespace语句.</p>
<p>Namespace关键字给一块代码命名. 在这个代码块外部,脚本必须用操作符::加上命名空间的名称来引用这个代码块. 引用静态的类成员也是用相同的方法. 在命名空间内代码不需要声明命名空间, 它本身就是默认的. 这种方法比添加前缀的方法好. 你的代码可由此变得更加紧凑和可读.</p>
<p>你可能想知道是否可以建立分层的(嵌套的)命名空间. 答案是不可以. 但你可以在命名空间名称后加上冒号, 你可以再次调用在名称中不包含冒号的变量,函数和类. 命名空间允许存在冒号,只要不是第一个字符和最后一个字符或接着另一个冒号. 命名空间的名称中的冒号对于PHP来说没有任何意义, 但如果你用他们来区分逻辑上的区块, 他们可以很好地说明你的代码中的父子(parent-child)关系.</p>
<p>注: 即可以使用这样: </p>
<table border="1" cellspacing="0" cellpadding="2" width="400" align="center">
<tbody>
<tr>
<td class="code" bgcolor="#e6e6e6">
<pre>namespace animal:dog {}
namespace animal:pig {}</pre>
</td>
</tr>
</tbody>
</table>
<p>用冒号来说明parent-child关系。</p>
<p>你可能在一个命名空间语句内没有包含函数,类或常量定义以外的任何东西. 这将阻止你使用他们来改进旧的使用全局变量的函数库. 命名空间最适合于面向对象. 命名空间内的常量与类中的常量使用相同的语法.</p>
<p>例子1显示了如何使用命名空间.</p>
<p>Using a namespace</p>
<table border="1" cellspacing="0" cellpadding="2" width="400" align="center">
<tbody>
<tr>
<td class="code" bgcolor="#e6e6e6">
<pre>＜?php
   namespace core_php:utility
   {
       class textEngine
       {
           public function uppercase($text) //大写
           {
               return(strtoupper($text));
           }
       } 

       //make non-OO interface 建立一个非OO的接口
       function uppercase($text)
       {
           $e = new textEngine;
           return($e-&gt;uppercase($text));
       } 

   } 

   //test class in namespace 测试命名空间中的类
   $e = new core_php:utility::textEngine;
   print($e-&gt;uppercase("from object") . "
"); 

   //test function in namespace 测试命名空间中的函数
   print(core_php:utility::uppercase("from function") . "
"); 

   //bring class into global namespace 把类导入全局命名空间
   import class textEngine from core_php:utility;
   $e2 = new textEngine;
?＞</pre>
</td>
</tr>
</tbody>
</table>
<p align="center"><span style="font-size: x-small;">例子1</span></p>
<p>Import语句把命名空间中的某个部份导入全局的命名空间.</p>
<p>要导入单一的命名空间的成员,可以指定类型为constant,function或class,接着写上成员的名称;</p>
<p>//如import class XXX</p>
<p>如果你想导入某一特定类型的所有成员,你可以用*来代替名称;</p>
<p>//如 import constant * 导入所有常量</p>
<p>如果你想导入所有类型的所有成员,用*即可.</p>
<p>//如 import *</p>
<p>在成员之后,用from关键字加上命名空间的名称.</p>
<p>//如 import class textEngine from core_php:utility;</p>
<p>总之你要写成像import * from myNamespace或 import class textEngine from core_php:utility这样的语句,就像例图1中那样.</p></div>
<div><a class="font_a" style="font-size: 12px;" href="http://ad.cn.doubleclick.net/clk;202930372;27012168;u" target="_blank"></a></div>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="第十四节--命名空间 -- Classes and Objects in PHP5 [14]" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">第十四节--命名空间 -- Classes and Objects in PHP5 [14]</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="PHP命名空间的分隔符是“\”" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP命名空间的分隔符是“\”</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="从php核心代码看require和include的区别" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4311&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4027588.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">从php核心代码看require和include的区别</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="PHP验证码：随机扭曲、旋转、变形（仿Google验证码）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4354&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4027581.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP验证码：随机扭曲、旋转、变形（仿Google验证码）</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/599/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>第十四节--命名空间 -- Classes and Objects in PHP5 [14]</title>
		<link>http://blog.haohtml.com/archives/597</link>
		<comments>http://blog.haohtml.com/archives/597#comments</comments>
		<pubDate>Thu, 13 Nov 2008 00:51:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[命名空间]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=597</guid>
		<description><![CDATA[第十四节--命名空间

命名变量,函数和类是挺困难的,除了要考虑到变量的名称要易于理解,还要担心是否这个名称在其它某个地方已经使用过. 在一小段脚本中,第二个问题是基本问题. 当你考虑重用你的代码, 在这之后的项目代码必须避免使用你用过的命名. 通常来说,可重用的代码总是包含在函数或类中, 需要处理许多可能发生的命名冲突. 但函数和类之间也可能发生命名冲突. 你可以尝试避免出现这种情况,通过在所有类前添加前缀的方法,或者你可以使用namespace语句.

Namespace关键字给一块代码命名. 在这个代码块外部,脚本必须用操作符::加上命名空间的名称来引用这个代码块. 引用静态的类成员也是用相同的方法. 在命名空间内代码不需要声明命名空间, 它本身就是默认的. 这种方法比添加前缀的方法好. 你的代码可由此变得更加紧凑和可读.<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="PHP命名空间的分隔符是“\”" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP命名空间的分隔符是“\”</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="PHP的命名空间" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP的命名空间</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="50个非常有用的PHP工具" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F7059&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/02/20541028.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">50个非常有用的PHP工具</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="PHP验证码：随机扭曲、旋转、变形（仿Google验证码）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4354&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4027581.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP验证码：随机扭曲、旋转、变形（仿Google验证码）</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>作者:Leon Atkinson 翻译：Haohappy 来源:超越PHP<br />
/*<br />
+-------------------------------------------------------------------------------+<br />
| = 本文为Haohappy读&lt;&lt;Core PHP Programming&gt;&gt;<br />
| = 中Classes and Objects一章的笔记<br />
| = 翻译为主+个人心得<br />
| = 为避免可能发生的不必要的麻烦请勿转载,谢谢<br />
| = 欢迎批评指正,希望和所有PHP爱好者共同进步!<br />
| = PHP5研究中心： http://blog.csdn.net/haohappy2004<br />
+-------------------------------------------------------------------------------+<br />
*/</p>
<p><strong>命名空间在PHP5正式版本中被取消，本节内容无效--Haohappy注</strong></p>
<p><strong>第十四节--命名空间</strong></p>
<p>命名变量,函数和类是挺困难的,除了要考虑到变量的名称要易于理解,还要担心是否这个名称在其它某个地方已经使用过. 在一小段脚本中,第二个问题是基本问题. 当你考虑重用你的代码, 在这之后的项目代码必须避免使用你用过的命名. 通常来说,可重用的代码总是包含在函数或类中, 需要处理许多可能发生的命名冲突. 但函数和类之间也可能发生命名冲突. 你可以尝试避免出现这种情况,通过在所有类前添加前缀的方法,或者你可以使用namespace语句.</p>
<p>Namespace关键字给一块代码命名. 在这个代码块外部,脚本必须用操作符::加上命名空间的名称来引用这个代码块. 引用静态的类成员也是用相同的方法. 在命名空间内代码不需要声明命名空间, 它本身就是默认的. 这种方法比添加前缀的方法好. 你的代码可由此变得更加紧凑和可读.</p>
<p>你可能想知道是否可以建立分层的(嵌套的)命名空间. 答案是不可以. 但你可以在命名空间名称后加上冒号, 你可以再次调用在名称中不包含冒号的变量,函数和类. 命名空间允许存在冒号,只要不是第一个字符和最后一个字符或接着另一个冒号. 命名空间的名称中的冒号对于PHP来说没有任何意义, 但如果你用他们来区分逻辑上的区块, 他们可以很好地说明你的代码中的父子(parent-child)关系.</p>
<p>/* 注: 即可以使用这样:<br />
namespace animal:dog {}<br />
namespace animal:pig {}</p>
<p>用冒号来说明parent-child关系.<br />
*/</p>
<p>你可能在一个命名空间语句内没有包含函数,类或常量定义以外的任何东西. 这将阻止你使用他们来改进旧的使用全局变量的函数库. 命名空间最适合于面向对象. 命名空间内的常量与类中的常量使用相同的语法.</p>
<p>例子6.17显示了如何使用命名空间.</p>
<p>Listing 6.17 Using a namespace</p>
<pre class="php">&lt;?php
   namespace core_php:utility
   {
       class textEngine
       {
           public function uppercase($text) //大写
           {
               return(strtoupper($text));
           }
       } 

       //make non-OO interface 建立一个非OO的接口
       function uppercase($text)
       {
           $e = new textEngine;
           return($e-&gt;uppercase($text));
       } 

   } 

   //test class in namespace 测试命名空间中的类
   $e = new core_php:utility::textEngine;
   print($e-&gt;uppercase("from object") . "&lt;br&gt;"); 

   //test function in namespace 测试命名空间中的函数
   print(core_php:utility::uppercase("from function") . "&lt;br&gt;"); 

   //bring class into global namespace 把类导入全局命名空间
   import class textEngine from core_php:utility;
   $e2 = new textEngine;
?&gt;</pre>
<p>Import语句把命名空间中的某个部份导入全局的命名空间.<br />
要导入单一的命名空间的成员,可以指定类型为constant,function或class,接着写上成员的名称;<br />
//如import class XXX<br />
如果你想导入某一特定类型的所有成员,你可以用*来代替名称;<br />
//如 import constant * 导入所有常量<br />
如果你想导入所有类型的所有成员,用*即可.<br />
//如 import *</p>
<p>在成员之后,用from关键字加上命名空间的名称.<br />
//如 import class textEngine from core_php:utility;</p>
<p>总之你要写成像import * from myNamespace或 import class textEngine from core_php:utility这样的语句,就像例6.17中那样.</p>
<p><span style="color: #ff0000;">注：本文章为原创文章，版权归文章作者与超越PHP网站所有，未经本站同意，禁止任何商业转载。非盈利网站及个人网站转载请注明出处，谢谢合作!</span></p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="PHP命名空间的分隔符是“\”" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F604&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP命名空间的分隔符是“\”</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="PHP的命名空间" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F599&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP的命名空间</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="50个非常有用的PHP工具" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F7059&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/02/20541028.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">50个非常有用的PHP工具</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="PHP验证码：随机扭曲、旋转、变形（仿Google验证码）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F4354&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F597">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4027581.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">PHP验证码：随机扭曲、旋转、变形（仿Google验证码）</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/597/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

