BaseObject
API reference › yii › base › BaseObject
34 subclasses
BehaviorComponentEventyii\caching\Dependencyyii\caching\MemCacheServeryii\data\Paginationyii\data\Sortyii\db\ColumnSchemayii\db\ColumnSchemaBuilderyii\db\Constraintyii\db\DataReaderyii\db\Expressionyii\db\QueryBuilderyii\db\Schemayii\db\SqlTokenyii\db\TableSchemayii\db\Transactionyii\grid\Columnyii\mail\BaseMessageyii\rbac\Assignmentyii\rbac\Itemyii\rbac\Ruleyii\web\AssetBundleyii\web\CompositeUrlRuleyii\web\Cookieyii\web\CookieCollectionyii\web\HeaderCollectionyii\web\JsExpressionyii\web\Linkyii\web\MultipartFormDataParseryii\web\UploadedFileyii\web\UrlNormalizeryii\web\UrlRuleyii2\extensions\bootstrap6\IconSet
BaseObject is the base class that implements the property feature.
A property is defined by a getter method (e.g. getLabel), and/or a setter method (e.g. setLabel). For example,
the following getter and setter methods define a property named label:
private $_label;
public function getLabel(){ return $this->_label;}
public function setLabel($value){ $this->_label = $value;}Property names are case-insensitive.
A property can be accessed like a member variable of an object. Reading or writing a property will cause the invocation of the corresponding getter or setter method. For example,
// equivalent to $label = $object->getLabel();$label = $object->label;// equivalent to $object->setLabel('abc');$object->label = 'abc';If a property has only a getter method and has no setter method, it is considered as read-only. In this case, trying to modify the property value will cause an exception.
One can call hasProperty(), canGetProperty() and/or canSetProperty() to check the existence of a property.
Besides the property feature, BaseObject also introduces an important object initialization life cycle. In particular, creating an new instance of BaseObject or its derived class will involve the following life cycles sequentially:
- the class constructor is invoked;
- object properties are initialized according to the given configuration;
- the
init()method is invoked.
In the above, both Step 2 and 3 occur at the end of the class constructor. It is recommended that
you perform object initialization in the init() method because at that stage, the object configuration
is already applied.
In order to ensure the above life cycles, if a child class of BaseObject needs to override the constructor, it should be done like the following:
public function __construct($param1, $param2, ..., $config = []){ ... parent::__construct($config);}That is, a $config parameter (defaults to []) should be declared as the last parameter
of the constructor, and the parent implementation should be called at the end of the constructor.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | BaseObject |
__call() | Calls the named method which is not a class method. | BaseObject |
__get() | Returns the value of an object property. | BaseObject |
__isset() | Checks if a property is set, i.e. defined and not null. | BaseObject |
__set() | Sets value of an object property. | BaseObject |
__unset() | Sets an object property to null. | BaseObject |
canGetProperty() | Returns a value indicating whether a property can be read. | BaseObject |
canSetProperty() | Returns a value indicating whether a property can be set. | BaseObject |
className() static | Returns the fully qualified name of this class. | BaseObject |
hasMethod() | Returns a value indicating whether a method is defined. | BaseObject |
hasProperty() | Returns a value indicating whether a property is defined. | BaseObject |
init() | Initializes the object. | BaseObject |
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(array<string, mixed> $config = [])Constructor.
The default implementation does two things:
- Initializes the object with the given configuration
$config. - Call
init().
If this method is overridden in a child class, it is recommended that
- the last parameter of the constructor is a configuration array, like
$confighere. - call the parent implementation at the end of the constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | array<string, | Name-value pairs that will be used to initialize the object properties |
__call()
Section titled “__call() ”public function __call(string $name, array $params): mixedCalls 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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The method name |
$params | array | Method parameters |
Returns
mixed: The method return value
Throws
UnknownMethodException: when calling unknown method
__get()
Section titled “__get() ”public function __get(string $name): mixedReturns the value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The property name |
Returns
mixed: The property value
Throws
UnknownPropertyException: if the property is not definedInvalidCallException: if the property is write-only
See also
__isset()
Section titled “__isset() ”public function __isset(string $name): boolChecks if a property is set, i.e. defined and not null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($object->property).
Note that if the property is not defined, false will be returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The property name or the event name |
Returns
bool: Whether the named property is set (not null).
See also
__set()
Section titled “__set() ”public function __set(string $name, mixed $value): mixedSets value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The property name or the event name |
$value | mixed | The property value |
Returns
mixed
Throws
UnknownPropertyException: if the property is not definedInvalidCallException: if the property is read-only
See also
__unset()
Section titled “__unset() ”public function __unset(string $name): mixedSets an object property to null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($object->property).
Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The property name |
Returns
mixed
Throws
InvalidCallException: if the property is read only.
See also
canGetProperty()
Section titled “canGetProperty() ”public function canGetProperty(string $name, bool $checkVars = true): boolReturns 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
$checkVarsis true);
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The property name |
$checkVars | bool | Whether to treat member variables as properties |
Returns
bool: Whether the property can be read
See also
canSetProperty()
Section titled “canSetProperty() ”public function canSetProperty(string $name, bool $checkVars = true): boolReturns 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
$checkVarsis true);
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The property name |
$checkVars | bool | Whether to treat member variables as properties |
Returns
bool: Whether the property can be written
See also
className()
Section titled “className() ”public static function className(): stringReturns the fully qualified name of this class.
Returns
string: The fully qualified name of this class.
hasMethod()
Section titled “hasMethod() ”public function hasMethod(string $name): boolReturns 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().
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The method name |
Returns
bool: Whether the method is defined
hasProperty()
Section titled “hasProperty() ”public function hasProperty(string $name, bool $checkVars = true): boolReturns 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
$checkVarsis true);
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The property name |
$checkVars | bool | Whether to treat member variables as properties |
Returns
bool: Whether the property is defined
See also
init()
Section titled “init() ”public function init(): mixedInitializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
Returns
mixed