<?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; 面向对象</title>
	<atom:link href="http://btmao.org/tag/%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1/feed/" rel="self" type="application/rss+xml" />
	<link>http://btmao.org</link>
	<description>B小T的幸福生活</description>
	<lastBuildDate>Thu, 06 Oct 2011 02:54:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<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 [...]]]></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>
	</channel>
</rss>

