<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.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>http://www.jackxiang.com/post//</link>
<title><![CDATA[结合instanceof和abstract来遍历对象。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Sun, 11 Jan 2009 02:45:33 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	instanceof 运算符是 PHP 5 引进的。在此之前用 is_a()，但是 is_a() 已经过时了，最好用 instanceof。<br/>例子一：<br/><div class="code"><br/>&lt;?php<br/>class A &#123; &#125;<br/>class B &#123; &#125;<br/><br/>$thing = new A;<br/><br/>if ($thing instanceof A) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;A&#039;;<br/>&#125;<br/>if ($thing instanceof B) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;B&#039;;<br/>&#125;<br/>?&gt; </div><br/>显示：A<br/><br/>例子二：<br/><div class="code">&lt;?php<br/>abstract class A &#123; &#125;<br/>abstract class B extends A&#123; &#125;<br/>class C extends B&#123;&#125;<br/>$thing = new C;<br/><br/>if ($thing instanceof A) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;A&#039;;<br/>&#125;<br/>if ($thing instanceof B) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;B&#039;;<br/>&#125;<br/>?&gt; </div><br/>显示：AB<br/><br/>参看下面代码用到上面的例子二在：&nbsp;&nbsp; foreach($myCollection as $s)&nbsp;&nbsp; ----》 if($s instanceof Shape) //如果$s是Shape类的实例 <br/><div class="code">&lt;?php <br/>&nbsp;&nbsp; //abstract root class 抽象根类 <br/>&nbsp;&nbsp; abstract class Shape <br/>&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abstract function getArea(); //定义一个抽象方法 <br/>&nbsp;&nbsp; &#125; <br/><br/>&nbsp;&nbsp; //abstract child class 抽象子类 <br/>&nbsp;&nbsp; abstract class Polygon extends Shape //多边形 <br/>&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abstract function getNumberOfSides(); <br/>&nbsp;&nbsp; &#125; <br/><br/>&nbsp;&nbsp; //concrete class 实体类 三角形类 <br/>&nbsp;&nbsp; class Triangle extends Polygon <br/>&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public $base; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public $height; <br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public function getArea() //覆写计算面积方法 <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(($this-&gt;base * $this-&gt;height)/2); <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125; <br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public function getNumberOfSides() //覆写边数统计方法 <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(3); <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125; <br/>&nbsp;&nbsp; &#125; <br/><br/>&nbsp;&nbsp; //concrete class 实体类四边形 <br/>&nbsp;&nbsp; class Rectangle extends Polygon <br/>&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public $width; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public $height; <br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public function getArea() <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return($this-&gt;width * $this-&gt;height); <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125; <br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public function getNumberOfSides() <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(4); <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125; <br/>&nbsp;&nbsp; &#125; <br/><br/>&nbsp;&nbsp; //concrete class 实体类 圆形 <br/>&nbsp;&nbsp; class Circle extends Shape <br/>&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public $radius; <br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public function getArea() <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(pi() * $this-&gt;radius * $this-&gt;radius); <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125; <br/>&nbsp;&nbsp; &#125; <br/><br/>&nbsp;&nbsp; //concrete root class 定义一个颜色类 <br/>&nbsp;&nbsp; class Color <br/>&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public $name; <br/>&nbsp;&nbsp; &#125; <br/><br/>&nbsp;&nbsp; $myCollection = array(); //建立形状的集合,放入数组 <br/><br/>&nbsp;&nbsp; //make a rectangle <br/>&nbsp;&nbsp; $r = new Rectangle; <br/>&nbsp;&nbsp; $r-&gt;width = 5; <br/>&nbsp;&nbsp; $r-&gt;height = 7; <br/>&nbsp;&nbsp; $myCollection&#91;&#93; = $r; <br/>&nbsp;&nbsp; unset($r); <br/><br/>&nbsp;&nbsp; //make a triangle <br/>&nbsp;&nbsp; $t = new Triangle; <br/>&nbsp;&nbsp; $t-&gt;base = 4; <br/>&nbsp;&nbsp; $t-&gt;height = 5; <br/>&nbsp;&nbsp; $myCollection&#91;&#93; = $t; <br/>&nbsp;&nbsp; unset($t); <br/><br/>&nbsp;&nbsp; //make a circle <br/>&nbsp;&nbsp; $c = new Circle; <br/>&nbsp;&nbsp; $c-&gt;radius = 3; <br/>&nbsp;&nbsp; $myCollection&#91;&#93; = $c; <br/>&nbsp;&nbsp; unset($c); <br/><br/>&nbsp;&nbsp; //make a color <br/>&nbsp;&nbsp; $c = new Color; <br/>&nbsp;&nbsp; $c-&gt;name = &quot;blue&quot;; <br/>&nbsp;&nbsp; $myCollection&#91;&#93; = $c; <br/>&nbsp;&nbsp; unset($c); <br/><br/>&nbsp;&nbsp; foreach($myCollection as $s) <br/>&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if($s instanceof Shape) //如果$s是Shape类的实例 <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(&quot;Area: &quot; . $s-&gt;getArea() . <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;&lt;br&gt;&#92;n&quot;); <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125; <br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if($s instanceof Polygon) <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(&quot;Sides: &quot; . <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $s-&gt;getNumberOfSides() . <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;&lt;br&gt;&#92;n&quot;); <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125; <br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if($s instanceof Color) <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(&quot;Color: $s-&gt;name&lt;br&gt;&#92;n&quot;); <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125; <br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(&quot;&lt;br&gt;&#92;n&quot;); <br/>&nbsp;&nbsp; &#125; <br/><br/>?&gt; </div><br/>解释：<br/>Shape:有一个取得面积的虚方法getArea，Polygon在继承了Shape的取得面积方法后添加了一个取得边数的方法getNumberOfSides，在下面的三角形四边形等都有边，继承重写Polygon（有边有面积），而圆形circle则只能是有面积没有几条边，直接继承了Shape和它的虚方法getArea,然后重写这个圆的面积计算方法。
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] 结合instanceof和abstract来遍历对象。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>