为了扩展Berry的答案,将访问级别设置为protected允许__get和__set与显式声明的属性一起使用(至少在类外部访问时),并且速度相当慢,我将引用另一个问题的评论关于这个主题,并为使用它提供理由:
我同意__get到自定义get函数(做相同的事情)的速度更慢,这是__get()的时间为0.0124455,而这0.0024445是10000次循环后用于自定义get()的时间。– Melsi 2012年11月23日,22 : 32 最佳做法:PHP魔术方法__set和__get
根据梅尔西(Melsi)的测试,慢得多,大约慢5倍。这绝对慢得多,但是还要注意,测试表明,您仍然可以使用此方法访问属性10,000次,计算循环迭代的时间(大约1/100秒)。与定义的实际get和set方法相比,它要慢得多,这是一种轻描淡写的方法,但是在总体方案中,即使慢5倍也从未真正变慢。
在99%的实际应用中,运算的计算时间仍然可以忽略不计,因此不值得考虑。唯一应该避免的时间是您实际上将在单个请求中访问该属性超过10,000次。如果高流量站点负担不起增加更多服务器以保持其应用程序运行的能力,那么这的确是错的。在访问率成为问题的高流量网站的页脚上放置一行文字广告,可能会为拥有该行文字的1000台服务器场支付费用。最终用户永远不会轻敲手指,不知道花这么长时间才能加载页面,因为您的应用程序的属性访问需要花费一百万分之一秒的时间。
我说这是来自.NET的开发人员,但对于消费者而言,无形的get和set方法并不是.NET的发明。它们根本就不是没有它们的属性,这些魔术方法是PHP开发人员的节省之选,甚至可以将其属性的版本全部称为“ properties”。另外,我认为,针对PHP的Visual Studio扩展确实支持具有受保护属性的智能感知,请牢记这一技巧。我认为,如果有足够的开发人员以这种方式使用神奇的__get和__set方法,PHP开发人员将调整执行时间以迎合开发人员社区。
编辑:从理论上讲,受保护的属性似乎可以在大多数情况下使用。实际上,事实证明,在访问类定义和扩展类中的属性时,很多时候您会想要使用getter和setter方法。更好的解决方案是扩展其他类时的基类和接口,因此您只需将几行代码从基类复制到实现类中即可。我要在项目的基类上做更多的工作,因此现在没有接口可以提供,但这是未经测试的精简类定义,其中包含使用反射来删除和移动属性的魔术属性并进行设置受保护的数组:
/** Base class with magic property __get() and __set() support for defined properties. */
class Component {
/** Gets the properties of the class stored after removing the original
* definitions to trigger magic __get() and __set() methods when accessed. */
protected $properties = array();
/** Provides property get support. Add a case for the property name to
* expand (no break;) or replace (break;) the default get method. When
* overriding, call parent::__get($name) first and return if not null,
* then be sure to check that the property is in the overriding class
* before doing anything, and to implement the default get routine. */
public function __get($name) {
$caller = array_shift(debug_backtrace());
$max_access = ReflectionProperty::IS_PUBLIC;
if (is_subclass_of($caller['class'], get_class($this)))
$max_access = ReflectionProperty::IS_PROTECTED;
if ($caller['class'] == get_class($this))
$max_access = ReflectionProperty::IS_PRIVATE;
if (!empty($this->properties[$name])
&& $this->properties[$name]->class == get_class()
&& $this->properties[$name]->access <= $max_access)
switch ($name) {
default:
return $this->properties[$name]->value;
}
}
/** Provides property set support. Add a case for the property name to
* expand (no break;) or replace (break;) the default set method. When
* overriding, call parent::__set($name, $value) first, then be sure to
* check that the property is in the overriding class before doing anything,
* and to implement the default set routine. */
public function __set($name, $value) {
$caller = array_shift(debug_backtrace());
$max_access = ReflectionProperty::IS_PUBLIC;
if (is_subclass_of($caller['class'], get_class($this)))
$max_access = ReflectionProperty::IS_PROTECTED;
if ($caller['class'] == get_class($this))
$max_access = ReflectionProperty::IS_PRIVATE;
if (!empty($this->properties[$name])
&& $this->properties[$name]->class == get_class()
&& $this->properties[$name]->access <= $max_access)
switch ($name) {
default:
$this->properties[$name]->value = $value;
}
}
/** Constructor for the Component. Call first when overriding. */
function __construct() {
// Removing and moving properties to $properties property for magic
// __get() and __set() support.
$reflected_class = new ReflectionClass($this);
$properties = array();
foreach ($reflected_class->getProperties() as $property) {
if ($property->isStatic()) { continue; }
$properties[$property->name] = (object)array(
'name' => $property->name, 'value' => $property->value
, 'access' => $property->getModifier(), 'class' => get_class($this));
unset($this->{$property->name}); }
$this->properties = $properties;
}
}
如果代码中有任何错误,我深表歉意。