class Object
Object类实现了类的多种特性
public function __get($name)__get
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
}
php魔术方法,当试图读取一个对象并不存在的属性的时候被调用。
e.g. $value = $object->property的时候,检查$object是否有getProperty方法,如果有,则返回getProperty(),如果没有,检查setProperty()方法是否存在。如果存在,抛出只写异常。如不存在,抛出未知属性异常。
public function __set($name, $value)__set
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
php魔术方法,当时图写入一个对象不存在的属性的时候被调用。
e.g. $object->property = $value的时候,检查$object是否有setProperty方法,如果有,则调用$object->setProperty($value)方法,没有检查是否有getProperty方法。如果存在,抛出只读异常。否则,抛出未知属性异常。
public function __isset($name)__isset
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
} else {
return false;
}
}
php魔术方法,isset($object->property)的时候调用。判断是否存在 $object->getProperty方法。
public function __unset($name)__unset
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
php魔术方法,unset($object->property)的时候调用。如果存在$object->setProperty方法,则调用$object->setProperty(null)。如不存在,抛出InvalidCallException异常。
/**__call
* Calls the named method which is not a class method.
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when an unknown method is being invoked.
* @param string $name the method name
* @param array $params method parameters
* @throws UnknownMethodException when calling unknown method
* @return mixed the method return value
*/
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
php魔术方法,执行一个未知方法的时候调用,直接抛出UnknownMethodException异常。
/**hasMethod
* Returns a value indicating whether a method is defined.
*
* The default implementation is a call to php function `method_exists()`.
* You may override this method when you implemented the php magic method `__call()`.
* @param string $name the property name
* @return boolean whether the property is defined
*/
public function hasMethod($name)
{
return method_exists($this, $name);
}
方法是否存在
/**canGetProperty canSetProperty
* Returns a value indicating whether a property can be read.
* A property is readable if:
*
* - the class has a getter method associated with the specified name
* (in this case, property name is case-insensitive);
* - the class has a member variable with the specified name (when `$checkVars` is true);
*
* @param string $name the property name
* @param boolean $checkVars whether to treat member variables as properties
* @return boolean whether the property can be read
* @see canSetProperty()
*/
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
/**
* Returns a value indicating whether a property can be set.
* A property is writable if:
*
* - the class has a setter method associated with the specified name
* (in this case, property name is case-insensitive);
* - the class has a member variable with the specified name (when `$checkVars` is true);
*
* @param string $name the property name
* @param boolean $checkVars whether to treat member variables as properties
* @return boolean whether the property can be written
* @see canGetProperty()
*/
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
判断getProperty(setProperty)方法是否存在。$checkVars参数为是否使用property_exists检查属性是否存在,默认为true。
/**hasProperty
* Returns a value indicating whether a property is defined.
* A property is defined if:
*
* - the class has a getter or setter method associated with the specified name
* (in this case, property name is case-insensitive);
* - the class has a member variable with the specified name (when `$checkVars` is true);
*
* @param string $name the property name
* @param boolean $checkVars whether to treat member variables as properties
* @return boolean whether the property is defined
* @see canGetProperty()
* @see canSetProperty()
*/
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
检查属性是否存在,可读或可写。