class Object
The object class implements multiple features of the class.
public function __get($name) { $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); } }_ Get
PHP magic method, called when trying to read an attribute that does not exist in an object.
E.g. $ value = $ object-> property, check whether $ object has the getproperty method. If yes, getproperty () is returned. If no, check whether the setproperty () method exists. If yes, a write-only exception is thrown. If not, an unknown attribute exception is thrown.
public function __set($name, $value) { $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); } }_ Set
PHP magic method, called when a graph is written to an object that does not exist.
E.g. when $ object-> property = $ value, check whether $ object has the setproperty method. If yes, call $ object-> setproperty ($ value, the getproperty method is not checked. If yes, a read-only exception is thrown. Otherwise, an unknown property exception is thrown.
public function __isset($name) { $getter = ‘get‘ . $name; if (method_exists($this, $getter)) { return $this->$getter() !== null; } else { return false; } }_ Isset
PHP magic method, called when isset ($ object-> property. Determine whether the $ object-> getproperty method exists.
public function __unset($name) { $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); } }_ Unset
PHP magic method, called when unset ($ object-> property. If the $ object-> setproperty method exists, call $ object-> setproperty (null ). If not, an invalidcallexception exception is thrown.
/** * 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()"); }_ Call
PHP magic method, called when an unknown method is executed, directly throws the unknownmethodexception exception.
/** * 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); }Hasmethod
Whether the method exists
/** * 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); }Cangetproperty cansetproperty
Determine whether the getproperty (setproperty) method exists. The $ checkvars parameter indicates whether to use property_exists to check whether the property exists. The default value is true.
/** * 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); }Hasproperty
Check whether the attribute exists, readable, or writable.
Yii2 Framework Analysis 2: Object Class