Skip to content
PHP Ecosystem
Explore

BaseObject

API reference › yii › base › BaseObject

classyii\base\BaseObject
ImplementsConfigurable
Since2.0.13
Sourceframework/base/BaseObject.php
34 subclasses

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:

  1. the class constructor is invoked;
  2. object properties are initialized according to the given configuration;
  3. 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.

MethodDescriptionDefined 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() staticReturns 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
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 $config here.
  • call the parent implementation at the end of the constructor.

Parameters

ParameterTypeDescription
$configarray<string, mixed>Name-value pairs that will be used to initialize the object properties
public function __call(string $name, array $params): mixed

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.

Parameters

ParameterTypeDescription
$namestringThe method name
$paramsarrayMethod parameters

Returns

mixed: The method return value

Throws

public function __get(string $name): mixed

Returns 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

ParameterTypeDescription
$namestringThe property name

Returns

mixed: The property value

Throws

See also

public function __isset(string $name): bool

Checks 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

ParameterTypeDescription
$namestringThe property name or the event name

Returns

bool: Whether the named property is set (not null).

See also

public function __set(string $name, mixed $value): mixed

Sets 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

ParameterTypeDescription
$namestringThe property name or the event name
$valuemixedThe property value

Returns

mixed

Throws

See also

public function __unset(string $name): mixed

Sets 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

ParameterTypeDescription
$namestringThe property name

Returns

mixed

Throws

See also

public function canGetProperty(string $name, bool $checkVars = true): bool

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);

Parameters

ParameterTypeDescription
$namestringThe property name
$checkVarsboolWhether to treat member variables as properties

Returns

bool: Whether the property can be read

See also

public function canSetProperty(string $name, bool $checkVars = true): bool

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);

Parameters

ParameterTypeDescription
$namestringThe property name
$checkVarsboolWhether to treat member variables as properties

Returns

bool: Whether the property can be written

See also

public static function className(): string

Returns the fully qualified name of this class.

Returns

string: The fully qualified name of this class.

public function hasMethod(string $name): bool

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().

Parameters

ParameterTypeDescription
$namestringThe method name

Returns

bool: Whether the method is defined

public function hasProperty(string $name, bool $checkVars = true): bool

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);

Parameters

ParameterTypeDescription
$namestringThe property name
$checkVarsboolWhether to treat member variables as properties

Returns

bool: Whether the property is defined

See also

public function init(): mixed

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

Returns

mixed