<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[php的动态代理的又一种实现]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Wed, 17 Mar 2010 08:42:29 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	 <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 本来php4里，在对象内部是可以对$this赋值的（或许是bug）。但php5不让这样了，如果对$this赋值会引发一个fatel error：can not re-asign to $this。今天突然想到了一个办法。既然不让对$this赋值，那能不能绕个圈实现呢？<br/>运行一下下面的代码<br/><br/><div class="code">&lt;?php<br/>class Container &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;public $foo;<br/>&nbsp;&nbsp;&nbsp;&nbsp;function __construct() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;foo=new Proxy($this);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&#125;<br/>class Proxy &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;private $container;<br/>&nbsp;&nbsp;&nbsp;&nbsp;function __construct($container) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;container=$container;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;function go() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;container-&gt;foo=10;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&#125;<br/>$c=new Container;<br/>echo &quot;开始&#92;n&quot;;<br/>echo &#039;gettype($c-&gt;foo) = &#92;&#039;&#039;.gettype($c-&gt;foo).&quot;&#039;&#92;n&quot;;<br/>echo &#039;get_class($c-&gt;foo) = &#92;&#039;&#039;.get_class($c-&gt;foo).&quot;&#039;&#92;n&quot;;<br/>echo &quot;然后&#92;n&quot;;<br/>$c-&gt;foo-&gt;go();<br/>echo &#039;gettype($c-&gt;foo) = &#92;&#039;&#039;.gettype($c-&gt;foo).&quot;&#039;&#92;n&quot;;<br/>echo &#039;$c-&gt;foo = &#039;.$c-&gt;foo.&quot;&#92;n&quot;;<br/>?&gt;</div>结果是：<br/><br/>开始<br/>gettype($c-&gt;foo) = &#039;object&#039;<br/>get_class($c-&gt;foo) = &#039;Proxy&#039;<br/>然后<br/>gettype($c-&gt;foo) = &#039;integer&#039;<br/>$c-&gt;foo = 10<br/><br/>可以看到，$c-&gt;foo已经从一个Proxy对象变成了一个整数，而这一切是在对象内部完成的。这样，我们就可以做一个清爽的动态代理了。<br/><br/><br/><div class="code">class Container &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;var $p;<br/>&nbsp;&nbsp;&nbsp;&nbsp;function __construct() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;p=new Proxy($this,&#039;p&#039;,&#039;Target&#039;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&#125;<br/><br/>class Target &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;var $v=777;<br/>&#125;<br/><br/>class Proxy &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;private $container;<br/>&nbsp;&nbsp;&nbsp;&nbsp;private $property;<br/>&nbsp;&nbsp;&nbsp;&nbsp;private $className;<br/>&nbsp;&nbsp;&nbsp;&nbsp;function __construct(&amp;amp;$container, $property, $class) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;container=&amp;amp;$container;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;property=$property;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;className=$class;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;function __get($key) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$class=$this-&gt;className;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$obj=new $class;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$property=$this-&gt;property;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;container-&gt;$property=$obj;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $obj-&gt;$key;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;function __call($func, $arg) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$class=$this-&gt;className;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$obj=new $class;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$property=$this-&gt;property;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;container-&gt;$property=$obj;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return call_user_func_array(array($obj,$func), $arg);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&#125;<br/><br/>$c=new Container;<br/>echo get_class($c-&gt;p).&quot;&#92;n&quot;;<br/>echo $c-&gt;p-&gt;v.&quot;&#92;n&quot;;<br/>echo get_class($c-&gt;p).&quot;&#92;n&quot;;<br/>echo $c-&gt;p-&gt;v.&quot;&#92;n&quot;;</div>可以看到，使用代理和使用实际对象没有区别，而且当第二次访问的时候，$c-&gt;p已经变成了实际的对象了。<br/>来源：http://syre.blogbus.com/logs/1882118.html
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] php的动态代理的又一种实现]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>