<?php
abstract class AbstractClass
{
public function __get($theName)
{
return (isset($this->$theName)) ? $this->$theName : NULL;
}
public function __set($theName, $theValue)
{
if (false === property_exists(get_class(), $theName)) {
throw new Exception(get_class()." does not have '".$theName."' property.");
} else {
$this->$theName = $theValue;
}
}
}
class ConcreteClass extends AbstractClass
{
private $x;
private $y;
public function __construct($theX, $theY)
{
$this->x = $theX;
$this->y = $theY;
}
}
$concreteClass = new ConcreteClass(10, 20);
var_dump($concreteClass->x);
是否有某種方法來完成這項工作,或者我將不得不將這些魔術方法添加到擴展類中?魔法方法(__get,__set)無法在擴展類中使用?
究竟出了什麼問題?是空的?你可能需要在基類中設置$ x和$ y才能工作。 – dqhendricks 2011-03-24 15:16:30