﻿<?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>Gavin&#039;s Blog &#187; PME</title>
	<atom:link href="http://laigw.name/tag/pme/feed" rel="self" type="application/rss+xml" />
	<link>http://laigw.name</link>
	<description>Keep it simple, stupid. Simplicity is beauty.</description>
	<lastBuildDate>Sun, 29 Jan 2012 07:14:51 +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沉思录之二：PME模型</title>
		<link>http://laigw.name/post/185.html</link>
		<comments>http://laigw.name/post/185.html#comments</comments>
		<pubDate>Tue, 18 Nov 2008 08:37:59 +0000</pubDate>
		<dc:creator>Gavin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PME]]></category>
		<category><![CDATA[PME模型]]></category>
		<category><![CDATA[沉思录]]></category>

		<guid isPermaLink="false">http://www.laigw.name/?p=185</guid>
		<description><![CDATA[本文发表在《程序员》第7期 PHP沉思录之二：PME模型 左轻侯 
在大规模的程序设计中，组件（component）已经成为一种非常流行的技术。常见的组件技术都基于PME模型，即属性（Property）、方法（Method）和事件（Event）。基于PME的组件技术可以方便地实现IoC（Inversion of Control，控制反转），是从IDE的plugin到应用服务器的“热发布”等许多技术的基础。
PHP从版本5开始，大大完善了对OO的支持，以前不能被应用的许多pattern现在都可以在PHP5中实现。因此，是否能够实现基于PHP的组件技术，也就成了一个值得讨论的问题。
下面对PHP对于PME模型的支持，逐一进行讨论：

1、属性（Property）
PHP并不支持类似Delphi或者C#的property语法，但这并不是问题。Java也不支持property语法，但是通过getXXX()和setXXX()的命名约定，同样可以支持属性。PHP也可以通过这一方式来支持属性。但是，PHP提供了另一种也许更好的方法，那就是__set()和__get()方法。
在PHP中，每一个class都会自动继承__set()和__get()方法。它们的定义如下：

&#160;查看代码 PHP1
2
void __set &#40; string name, mixed value &#41; 
mixed __get &#40; string name &#41;

这两个方法将在下列情况下被触发：当程序访问一个当前类没有显式定义的属性时。在这个时候，被访问的属性名称作为参数被传入相应的方法。任何类都可以重载__set()和__get()方法，以实现自己的功能。
如下例：

&#160;查看代码 PHP1
2
3
4
5
6
7
8
9
10
11
12
13
14
class PropertyTester &#123; 
&#160;
  public function __get&#40;$PropName&#41; &#123; 
    echo &#34;Getting Property $PropNamen&#34;; 
  &#125; 
&#160;
  public function __set&#40;$PropName, $Value&#41; &#123; 
    echo &#34;Setting Property $PropName [...]]]></description>
		<wfw:commentRss>http://laigw.name/post/185.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

