本来想晚点发上来的,但是为了解决问题只好先发上来了,一会儿再贴另个一篇吧!是关于接口的,本人也是初学者,只是测试改写稍加解释了这个,你没有觉得眼熟吗?那你肯定没有用功

有不对之处望各位指正,对的我贴上了,但上感觉那个没有赋值的还是有错,好像应该输出all 你们有空再看看吧!
再补上一点儿:
在C++中大家都知道一个类的每一个成员都隐含着一个所属类的指针参数,
其名字规定为this关键字,当一个对象调用一个成员函数时,除了把指定的实参传递给
形参外,还同还把该对象的地址或指针对象的值传递给成员函数中隐含的指针形参this
这里也是这个道理,虽然它不是指针,但你可以这么理解一下
class CITY
{
private $cityname=null;//个人认为最好也赋下初值的好,虽说系统也默认为NULL
public function __construct($cityname)//构造函数说白了也是初始化
{
// Load some city-specific data
$this->cityname=$cityname;//通过$this指针 给本身赋初
}
public function population($demographic = 'all')
{
if (!$this->countries[$code])
{
return $this->population[$demographic]=$demographic;
}
else
{
return $this->population[$demographic];
}
}
class COUNTRY
{
private $code = null;
private $cities = array();
public function __construct($code)
{
$this->code = $code;
}
public function city($cityname)
{
if (!$this->cities[$cityname])
{
$this->cities[$cityname] = new CITY($cityname);
}
return $this->cities[$cityname];
}
}
class WORLD
{
private $countries = array();
public function country($code = 'us')
{
if (!$this->countries[$code])
{
$this->countries[$code] = new COUNTRY($code);
}
return $this->countries[$code];
}
}
$world = new WORLD;
// Load the country AND city object
echo $world->country('us')->city('seattle')->population('employed');
// Country US is already loaded, only need to load a new city object.
echo $world->country('us')->city('new york')->population();
?>
[
本帖最后由 极品黑公子 于 2007-7-30 14:56 编辑 ]