<?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>BTMao &#187; PHP</title>
	<atom:link href="http://btmao.org/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://btmao.org</link>
	<description>B小T的幸福生活</description>
	<lastBuildDate>Thu, 11 Feb 2010 03:09:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>php的Socket</title>
		<link>http://btmao.org/2009/08/10/php_socket/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=php_socket</link>
		<comments>http://btmao.org/2009/08/10/php_socket/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 13:56:28 +0000</pubDate>
		<dc:creator>BTMao</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[socket]]></category>

		<guid isPermaLink="false">http://btmao.org/?p=146</guid>
		<description><![CDATA[当我们需要在不同服务器或者不同的语言之间做数据交换，我们有一种方法是用Socket做数据代理。Socket是源于UNIX的套接字，基于TCP/IP协议，是从UNIX早期的命令集中演化而来的，基础的模式是“连接-读写-关闭”。Socket可以应用于B/S和C/S两种不同的网络软件架构上，现在已经被广泛的引用。
php的Socket模块虽然相对比较简陋，一些复杂的应用会出现一些莫名奇妙的问题，但是单单作为基础的数据代理来讲还是经受了人们的检验。
一、Socket传输的是什么
Socket传输的是字节流，没有定义边界。只是通过调节缓冲区的大小来完成流的截断，当然设置缓冲区的大小都是必须的，否则Socket也许会一直工作下去。Socket传输的数据很容易被截取并且会直接展示出来，所以你通常需要对你的数据进行加密，比如AES，就是很好的加密算法，不要用MD5这样的散列表，否则你会死得很惨。
PHP似乎想要get字节流加上边界，在它的socket_write这个函数中声明的三个参数，它的文档里面说如果你将第三个参数设置为PHP_NORMAL_READ，那么将会读取到/r或者/n，可惜，当我这样做的时候，程序溢出了。原因至今不明。
二、用php做一个SocketServer
我们先抛弃类，这篇将没有一个类定义出现。
前面我们已经说了Socket的工作模式是“连接-读写-关闭”。作为服务器，我们要做的事情是建立一个socket，绑定一个端口，然后监视每一个连接，读取他们传来的消息，并给予他们反馈，然后将这个连接毙掉。现在我们开始

&#60; ?php
//===================
//SockServ.php
//===================
define&#40;'SOCKIP', 'localhost'&#41;;
define&#40;'SOCKPORT', '12345'&#41;;
&#160;
/**
 * Set up our socket
 */
$sock = socket_create&#40;AF_INET, SOCK_STREAM, SOL_TCP&#41;; //创建一个socket
printf&#40;&#34;Socket created.\r\n&#34;&#41;;
socket_bind&#40;$sock, SOCKIP, SOCKPORT&#41;; //绑定一个端口
socket_listen&#40;$sock&#41;; //开始监视了
printf&#40;&#34;Socket has set up.\r\n\r\n&#34;&#41;;
&#160;
while&#40;true&#41; &#123;
	$conn = socket_accept&#40;$sock&#41;; //抓到一个，允许它的连接
	if &#40;$conn&#41; &#123;
		printf&#40;&#34;==========================================\r\n&#34;&#41;;
		printf&#40;&#34;Socket connected.\r\n&#34;&#41;;
		while&#40;$data = socket_read&#40;$conn, 1024&#41;&#41; &#123; 
		//千万不能加上PHP_NORMAL_READ啊
			$buffer = $data;
			printf&#40;&#34;Data Received.\r\n&#34;&#41;;
			print_r&#40;&#34;Buffer: &#34;.$buffer.&#34;\r\n&#34;&#41;;
			socket_write&#40;$conn, &#34;OK&#34;&#41;; //告诉客户端，OK，我收到了
			printf&#40;&#34;Successed!\r\n&#34;&#41;;
		&#125;
		socket_close&#40;$conn&#41;; //工作完成，你可以去了
		printf&#40;&#34;Closed the socket\r\n&#34;&#41;;
		printf&#40;&#34;==========================================\r\n\r\n&#34;&#41;;
	&#125;
&#125;
?&#62;

上面这段程序，你最好别用浏览器来跑，你可能会得到一个超时，你应该在命令行里面跑。在服务器上，让它跑在后台，然后就让它呆在那里吧，没事的。
三、还有客户端呢
我们当然还需要一个客户端，不然你执行上面程序时会一直停在“Socket has set up.”。客户端的任务就是在浏览器里打开一张页面程序，向服务器端发送那么一个字符串，然后接受服务器返回的“OK”。我们开始吧

&#60; ?php
//===================
//SockClient.php
//===================
define&#40;'SOCKIP', 'localhost'&#41;;
define&#40;'SOCKPORT', '12345'&#41;;
&#160;
$sock = socket_create&#40;AF_INET, SOCK_STREAM, SOL_TCP&#41;; //创建一个socket
$connStat [...]]]></description>
			<content:encoded><![CDATA[<p>当我们需要在不同服务器或者不同的语言之间做数据交换，我们有一种方法是用Socket做数据代理。Socket是源于UNIX的套接字，基于TCP/IP协议，是从UNIX早期的命令集中演化而来的，基础的模式是“连接-读写-关闭”。Socket可以应用于B/S和C/S两种不同的网络软件架构上，现在已经被广泛的引用。<br />
php的Socket模块虽然相对比较简陋，一些复杂的应用会出现一些莫名奇妙的问题，但是单单作为基础的数据代理来讲还是经受了人们的检验。</p>
<p><strong>一、Socket传输的是什么</strong></p>
<p>Socket传输的是字节流，没有定义边界。只是通过调节缓冲区的大小来完成流的截断，当然设置缓冲区的大小都是必须的，否则Socket也许会一直工作下去。Socket传输的数据很容易被截取并且会直接展示出来，所以你通常需要对你的数据进行加密，比如AES，就是很好的加密算法，不要用MD5这样的散列表，否则你会死得很惨。<br />
PHP似乎想要get字节流加上边界，在它的socket_write这个函数中声明的三个参数，它的文档里面说如果你将第三个参数设置为PHP_NORMAL_READ，那么将会读取到/r或者/n，可惜，当我这样做的时候，程序溢出了。原因至今不明。</p>
<p><strong>二、用php做一个SocketServer</strong></p>
<blockquote><p>我们先抛弃类，这篇将没有一个类定义出现。</p></blockquote>
<p>前面我们已经说了Socket的工作模式是“连接-读写-关闭”。作为服务器，我们要做的事情是建立一个socket，绑定一个端口，然后监视每一个连接，读取他们传来的消息，并给予他们反馈，然后将这个连接毙掉。现在我们开始</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #666666; font-style: italic;">//===================</span>
<span style="color: #666666; font-style: italic;">//SockServ.php</span>
<span style="color: #666666; font-style: italic;">//===================</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SOCKIP'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SOCKPORT'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'12345'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Set up our socket
 */</span>
<span style="color: #000088;">$sock</span> <span style="color: #339933;">=</span> <span style="color: #990000;">socket_create</span><span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,</span> SOCK_STREAM<span style="color: #339933;">,</span> SOL_TCP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//创建一个socket</span>
<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Socket created.<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">socket_bind</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sock</span><span style="color: #339933;">,</span> SOCKIP<span style="color: #339933;">,</span> SOCKPORT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//绑定一个端口</span>
<span style="color: #990000;">socket_listen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sock</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//开始监视了</span>
<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Socket has set up.<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">socket_accept</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sock</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//抓到一个，允许它的连接</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;==========================================<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Socket connected.<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">socket_read</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		<span style="color: #666666; font-style: italic;">//千万不能加上PHP_NORMAL_READ啊</span>
			<span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Data Received.<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Buffer: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$buffer</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">socket_write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;OK&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//告诉客户端，OK，我收到了</span>
			<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Successed!<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #990000;">socket_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//工作完成，你可以去了</span>
		<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Closed the socket<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;==========================================<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>上面这段程序，你最好别用浏览器来跑，你可能会得到一个超时，你应该在命令行里面跑。在服务器上，让它跑在后台，然后就让它呆在那里吧，没事的。</p>
<p><strong>三、还有客户端呢</strong></p>
<p>我们当然还需要一个客户端，不然你执行上面程序时会一直停在“Socket has set up.”。客户端的任务就是在浏览器里打开一张页面程序，向服务器端发送那么一个字符串，然后接受服务器返回的“OK”。我们开始吧</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #666666; font-style: italic;">//===================</span>
<span style="color: #666666; font-style: italic;">//SockClient.php</span>
<span style="color: #666666; font-style: italic;">//===================</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SOCKIP'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SOCKPORT'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'12345'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sock</span> <span style="color: #339933;">=</span> <span style="color: #990000;">socket_create</span><span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,</span> SOCK_STREAM<span style="color: #339933;">,</span> SOL_TCP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//创建一个socket</span>
<span style="color: #000088;">$connStat</span> <span style="color: #339933;">=</span> <span style="color: #990000;">socket_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sock</span><span style="color: #339933;">,</span> SOCKIP<span style="color: #339933;">,</span> SOCKPORT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//连接到服务器</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$connStat</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//连接成功</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;p&gt;连接成功！'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Hi, Server!'</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">socket_write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sock</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">socket_read</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sock</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//依然切记不要PHP_NORMAL_READ</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'OK'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;p&gt;OK，搞定！&lt;/p&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">socket_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sock</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//你可以安心的去了</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//连接失败啦 </span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;p&gt;连接居然失败了，是不是服务器没开啊！&lt;/p&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>这个程序是跑在浏览器上面的，通常呢，我们会写成一个类这个类独立在系统的架构之外，将这个类文件交给负责不同app的程序员，让他们来调用，从而实现和服务器的通信。</p>
<p><strong>四、当然这是个很简单的例子</strong></p>
<p>实际上，我们通常有更复杂的事情去做，但是并不代表对于socket来说我们要做更多的事情，我们只需要在客户端的数据开源（现在是直接定义的），服务器端的数据处理（现在只是打印到屏幕上），还有服务器根据客户端数据进行不同的反馈以及客户端对反馈的处理，这几个方面进行扩展，便可以完成几乎所有的需求。<br />
Socket作为一种有效的数据代理方式，还能够为更好的程序架构提供帮助，比如我们提供更多的服务器数据交换来降低前段服务器的计算压力，Socket本身有很好的栈策略也能在服务器的部署上带来帮助。</p>
]]></content:encoded>
			<wfw:commentRss>http://btmao.org/2009/08/10/php_socket/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP的魔术函数们</title>
		<link>http://btmao.org/2009/02/26/magicfuncofphp/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=magicfuncofphp</link>
		<comments>http://btmao.org/2009/02/26/magicfuncofphp/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 14:58:00 +0000</pubDate>
		<dc:creator>BTMao</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[写程序]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[面向对象]]></category>
		<category><![CDATA[魔术函数]]></category>

		<guid isPermaLink="false">http://www.btmao.org/?p=37</guid>
		<description><![CDATA[PHP5带给我们的面向对象的变化是伟大的是空前的是胜利的，就像所有的两会一样。尤其是它提供给我们的那些魔术函数，更是像桂林山水一样令人流连忘返。
1. __construct() 构造函数

这是PHP默认的构造函数，它并不能主动被程序调用只能是在对象创建的时候被自动调用。同时为了向下兼容，PHP5也支持与类同名的构造函数，值得一提的是当两种构造函数同时存在的时候，__construct()将不会被调用。
2. __destruct() 析构函数
3. __get($key)
当读取一个不存在的属性时调用。

&#60; ?php
class Obj &#123;
	public function __get&#40;$key&#41; &#123;
		echo $key.' is not exsit';
	&#125;
&#125;
$obj = new Obj&#40;&#41;;
echo $obj-&#62;varnotexist;
?&#62;

上面的这段程序将会输出：varnotexist is not exist。
4. __set($key, value)
与__get($key)类似，而它是在修改一个不存在的属性时被调用。
5. __call($key, $args)
与__get($key)类似，而它是在调用一个不存在的方法时被调用。
6. __toString()
在试图打印一个对象时被调用

&#60; ?php
class Obj &#123;
	private $a;
	private $b;
	public function __construct&#40;&#41; &#123;
		$this-&#62;a = 'a';
		$this-&#62;b = 'b';
	&#125;
	public function __toString&#40;&#41; &#123;
		echo $this-&#62;a.'&#124;'.$this-&#62;b;
	&#125;
&#125;
$obj = new Obj&#40;&#41;;
echo $obj;
?&#62;

此时程序会打印：a&#124;b
7. __clone()
当对象被克隆的时候被打印。

&#60; ?php
class Obj &#123;
	public function __clone&#40;&#41; &#123;
		echo [...]]]></description>
			<content:encoded><![CDATA[<p>PHP5带给我们的面向对象的变化是伟大的是空前的是胜利的，就像所有的两会一样。尤其是它提供给我们的那些魔术函数，更是像桂林山水一样令人流连忘返。</p>
<p><strong>1. __construct() 构造函数<br />
</strong></p>
<p>这是PHP默认的构造函数，它并不能主动被程序调用只能是在对象创建的时候被自动调用。同时为了向下兼容，PHP5也支持与类同名的构造函数，值得一提的是当两种构造函数同时存在的时候，__construct()将不会被调用。</p>
<p><strong>2. __destruct() 析构函数</strong></p>
<p><strong>3. __get($key)</strong></p>
<p>当读取一个不存在的属性时调用。</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">class</span> Obj <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$key</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' is not exsit'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Obj<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">varnotexist</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>上面的这段程序将会输出：varnotexist is not exist。</p>
<p><strong>4. __set($key, value)</strong></p>
<p>与__get($key)类似，而它是在修改一个不存在的属性时被调用。</p>
<p><strong>5. __call($key, $args)</strong></p>
<p>与__get($key)类似，而它是在调用一个不存在的方法时被调用。</p>
<p><strong>6. __toString()</strong></p>
<p>在试图打印一个对象时被调用</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">class</span> Obj <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">b</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">a</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'|'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">b</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Obj<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>此时程序会打印：a|b</p>
<p><strong>7. __clone()</strong></p>
<p>当对象被克隆的时候被打印。</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">class</span> Obj <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __clone<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;">'another me'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Obj<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$obj1</span> <span style="color: #339933;">=</span> clone <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>程序输入：another me</p>
<p><strong>8. __sleep()</strong></p>
<p>该函数在对象进行序列化之前调用，它可以清除对象，并返回一个将被用于序列化的变量的数组。通常被用来做一些无需存储的数据和一些数据库连接的清理工作。</p>
<p><strong>9. __wakeup()</strong></p>
<p>与__sleep()正好相反，他在unserialize()之前被调用，通常用来重建一些变量和资源，或者用来初始化。</p>
<p><strong>10. __isset()</strong></p>
<p>在isset()函数执行前调用。</p>
<p><strong>11. __unset()</strong></p>
<p>在unset()函数执行前调用。</p>
<p><strong>12. __set_state()</strong></p>
<p>在var_export()前被调用，var_export()是一个被我忽视的函数，他和var_dump()一样返回的是一个变量的结构，不同的是他返回的是一个合法的PHP代码。比如：</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">class</span> Obj<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$a</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'apple'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$b</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'banana'</span><span style="color: #339933;">;</span>
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set_state<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'=&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Obj<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">var_export</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">';'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>程序将会输出：</p>

<div class="wp_syntax"><div class="code"><pre class="php">a<span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'apple'</span>
b<span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'banana'</span></pre></div></div>

<p>这有什么用呢，它可以让我们缓存或保存一个变量的结构并且随时使用。</p>
<p><strong>13. __autoload($key)</strong></p>
<p>在实例一个对象时，如果找不到相应的类的定义，则会调用。</p>
<p><strong>最后</strong></p>
<p>以上函数中__get(), __set(), __call(), __autoload()之类的函数也许会消耗相当多的系统资源，并不推荐使用。</p>
]]></content:encoded>
			<wfw:commentRss>http://btmao.org/2009/02/26/magicfuncofphp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP的MVC</title>
		<link>http://btmao.org/2009/02/26/mvcofphp/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=mvcofphp</link>
		<comments>http://btmao.org/2009/02/26/mvcofphp/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 16:33:05 +0000</pubDate>
		<dc:creator>BTMao</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[系统架构]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://www.btmao.org/?p=10</guid>
		<description><![CDATA[PHP的面向对象的特性越来越完善使得，PHP能够更好的从代码结构上进行系统的封装。当一个系统，其中有大量的逻辑存在与流程控制之中，MVC的优势就开始渐渐体现。
MVC的模式要求对系统的总体结构在逻辑上分成三部分

View（界面/视图）：用户界面，网页模板
Controller（控制/流程）：系统业务流程
Model（模型/逻辑）：系统设计中的一些基础类和数据操作

拿一个简单的登陆流程当例子
最常见的登陆流程是这样的，我们假设我们的网站根目录下有一个叫做/user的文件夹来存放所有与用户操作相关的页面，其中有一个页面是/user/login.php。而我们的根目录上有文件/index.php作为网站的首页。当我们打开/index.php的时候进行判断，如果未登陆则跳转到/user/login.php进行登陆，如果成功则跳回/index.php，否则跳到/error.php。
流程图如下：

根据文字和流程图的描述，我们可以开始编码了。
/index.php

&#60; ?php
//...
$userLib = new userLib&#40;&#41;;
if &#40;!$userLib-&#62;chkLogin&#40;&#41;&#41; header&#40;'location: /user/login.php'&#41;;
//...
?&#62;

/user/login.php

&#60; ?php
//...
if &#40;isset&#40;$_POST&#91;'submit'&#93;&#41;&#41; &#123;
	$userLib = new userLib&#40;&#41;;
	$succ = $userLib-&#62;login&#40;$_POST&#91;'name'&#93;, $_POST&#91;'pass'&#93;&#41;;
	header&#40;'location: '.&#40;$succ?'/index.php':'/error.php'&#41;&#41;;
&#125;
$tpl = new Template&#40;&#41;;
$tpl-&#62;show&#40;'user_login.tpl'&#41;;
?&#62;

/error.php

&#60; ?php
$tpl = new Template&#40;&#41;;
$tpl-&#62;show&#40;'error.tpl'&#41;;
?&#62;

关于Controller
就像上面三段程序就是系统中的controller，他们并不执行什么具体的操作也不输出，只是不断调用Model中的不同方法来处理逻辑。
关于Model
Model则是另一个重要的话题，那便是在PHP中如何更好的类封装。但是在这里我们看到的在/user/login.php中被使用的userLib类和Template类都是系统中的model，他们处理具体的数据操作，并返回值来用controller使用。
从PHP5开始，我们有了接口，从PHP5.3开始，虽然还没有Release，我们有了namespace，PHP真正面向对象的时代已经到来了。这时候我们已经没有必要再去讨论我们应该把userLib写成userLib.function.php还是userLib.class.php，我们要做的只是更好的封装和理解各model之间的从属逻辑结构。更好的设计model中的各个库来保障系统的运行效率和维护效率。
关于View
View的部分往往被看作是前段设计人员的工作，但是作为PHP的程序员也必须关注这一话题。
对于view和controller来说，所有的页面都是围绕着流程来产生的。而controller有时也担负着处理页面与页面之间相互传递的工作。我们要确定页面的元素，特别是表单元素，并通过表单元素来处理用户提交的数据，其次我们还要确定PHP传递给模板的变量，然后才能交付给设计人员。
最后
我们还经常用来一些常用的文件夹名和文件名来暗示程序的阅读者，特别是新的阅读者，该文件夹或文件的作用。这也是相当重要的。
]]></description>
			<content:encoded><![CDATA[<p>PHP的面向对象的特性越来越完善使得，PHP能够更好的从代码结构上进行系统的封装。当一个系统，其中有大量的逻辑存在与流程控制之中，MVC的优势就开始渐渐体现。</p>
<p>MVC的模式要求对系统的总体结构在逻辑上分成三部分</p>
<ol>
<li>View（界面/视图）：用户界面，网页模板</li>
<li>Controller（控制/流程）：系统业务流程</li>
<li>Model（模型/逻辑）：系统设计中的一些基础类和数据操作</li>
</ol>
<p><strong>拿一个简单的登陆流程当例子</strong></p>
<p>最常见的登陆流程是这样的，我们假设我们的网站根目录下有一个叫做/user的文件夹来存放所有与用户操作相关的页面，其中有一个页面是/user/login.php。而我们的根目录上有文件/index.php作为网站的首页。当我们打开/index.php的时候进行判断，如果未登陆则跳转到/user/login.php进行登陆，如果成功则跳回/index.php，否则跳到/error.php。</p>
<p>流程图如下：</p>
<p><img class="alignnone size-full wp-image-12" title="一个简单的登陆流程" src="http://www.btmao.org/wp-content/uploads/2009/02/e69caae6a087e9a298-1.jpg" alt="一个简单的登陆流程" width="451" height="339" /></p>
<p>根据文字和流程图的描述，我们可以开始编码了。</p>
<p>/index.php</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #666666; font-style: italic;">//...</span>
<span style="color: #000088;">$userLib</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> userLib<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$userLib</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">chkLogin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'location: /user/login.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//...</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>/user/login.php</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #666666; font-style: italic;">//...</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$userLib</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> userLib<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$succ</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$userLib</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">login</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'location: '</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$succ</span>?<span style="color: #0000ff;">'/index.php'</span><span style="color: #339933;">:</span><span style="color: #0000ff;">'/error.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$tpl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Template<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_login.tpl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>/error.php</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000088;">$tpl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Template<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error.tpl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>关于Controller</strong></p>
<p>就像上面三段程序就是系统中的controller，他们并不执行什么具体的操作也不输出，只是不断调用Model中的不同方法来处理逻辑。</p>
<p><strong>关于Model</strong></p>
<p>Model则是另一个重要的话题，那便是在PHP中如何更好的类封装。但是在这里我们看到的在/user/login.php中被使用的userLib类和Template类都是系统中的model，他们处理具体的数据操作，并返回值来用controller使用。</p>
<p>从PHP5开始，我们有了接口，从PHP5.3开始，虽然还没有Release，我们有了namespace，PHP真正面向对象的时代已经到来了。这时候我们已经没有必要再去讨论我们应该把userLib写成userLib.function.php还是userLib.class.php，我们要做的只是更好的封装和理解各model之间的从属逻辑结构。更好的设计model中的各个库来保障系统的运行效率和维护效率。</p>
<p><strong>关于View</strong></p>
<p>View的部分往往被看作是前段设计人员的工作，但是作为PHP的程序员也必须关注这一话题。</p>
<p>对于view和controller来说，所有的页面都是围绕着流程来产生的。而controller有时也担负着处理页面与页面之间相互传递的工作。我们要确定页面的元素，特别是表单元素，并通过表单元素来处理用户提交的数据，其次我们还要确定PHP传递给模板的变量，然后才能交付给设计人员。</p>
<p><strong>最后</strong></p>
<p>我们还经常用来一些常用的文件夹名和文件名来暗示程序的阅读者，特别是新的阅读者，该文件夹或文件的作用。这也是相当重要的。</p>
]]></content:encoded>
			<wfw:commentRss>http://btmao.org/2009/02/26/mvcofphp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
