# BaseObject

> BaseObject is the base class that implements the *property* feature.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [base](/docs/api/yii/base/index.md) › BaseObject

| class | `yii\base\BaseObject` |
| --- | --- |
| Implements | [`Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0.13 |
| Source | [`framework/base/BaseObject.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/base/BaseObject.php#L78) |

34 subclasses:

- [`Behavior`](/docs/api/yii/base/behavior/index.md)
- [`Component`](/docs/api/yii/base/component/index.md)
- [`Event`](/docs/api/yii/base/event/index.md)
- [`yii\caching\Dependency`](/docs/api/yii/caching/dependency/index.md)
- [`yii\caching\MemCacheServer`](/docs/api/yii/caching/memcacheserver/index.md)
- [`yii\data\Pagination`](/docs/api/yii/data/pagination/index.md)
- [`yii\data\Sort`](/docs/api/yii/data/sort/index.md)
- [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md)
- [`yii\db\ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md)
- [`yii\db\Constraint`](/docs/api/yii/db/constraint/index.md)
- [`yii\db\DataReader`](/docs/api/yii/db/datareader/index.md)
- [`yii\db\Expression`](/docs/api/yii/db/expression/index.md)
- [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md)
- [`yii\db\Schema`](/docs/api/yii/db/schema/index.md)
- [`yii\db\SqlToken`](/docs/api/yii/db/sqltoken/index.md)
- [`yii\db\TableSchema`](/docs/api/yii/db/tableschema/index.md)
- [`yii\db\Transaction`](/docs/api/yii/db/transaction/index.md)
- [`yii\grid\Column`](/docs/api/yii/grid/column/index.md)
- [`yii\mail\BaseMessage`](/docs/api/yii/mail/basemessage/index.md)
- [`yii\rbac\Assignment`](/docs/api/yii/rbac/assignment/index.md)
- [`yii\rbac\Item`](/docs/api/yii/rbac/item/index.md)
- [`yii\rbac\Rule`](/docs/api/yii/rbac/rule/index.md)
- [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md)
- [`yii\web\CompositeUrlRule`](/docs/api/yii/web/compositeurlrule/index.md)
- [`yii\web\Cookie`](/docs/api/yii/web/cookie/index.md)
- [`yii\web\CookieCollection`](/docs/api/yii/web/cookiecollection/index.md)
- [`yii\web\HeaderCollection`](/docs/api/yii/web/headercollection/index.md)
- [`yii\web\JsExpression`](/docs/api/yii/web/jsexpression/index.md)
- [`yii\web\Link`](/docs/api/yii/web/link/index.md)
- [`yii\web\MultipartFormDataParser`](/docs/api/yii/web/multipartformdataparser/index.md)
- [`yii\web\UploadedFile`](/docs/api/yii/web/uploadedfile/index.md)
- [`yii\web\UrlNormalizer`](/docs/api/yii/web/urlnormalizer/index.md)
- [`yii\web\UrlRule`](/docs/api/yii/web/urlrule/index.md)
- [`yii2\extensions\bootstrap6\IconSet`](/docs/extensions/bootstrap6/reference/iconset/index.md)

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()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty), [`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) and/or [`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-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.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Constructor. | `BaseObject` |
| [`__call()`](#method-__call) | Calls the named method which is not a class method. | `BaseObject` |
| [`__get()`](#method-__get) | Returns the value of an object property. | `BaseObject` |
| [`__isset()`](#method-__isset) | Checks if a property is set, i.e. defined and not null. | `BaseObject` |
| [`__set()`](#method-__set) | Sets value of an object property. | `BaseObject` |
| [`__unset()`](#method-__unset) | Sets an object property to null. | `BaseObject` |
| [`canGetProperty()`](#method-canGetProperty) | Returns a value indicating whether a property can be read. | `BaseObject` |
| [`canSetProperty()`](#method-canSetProperty) | Returns a value indicating whether a property can be set. | `BaseObject` |
| [`className()`](#method-className) static | Returns the fully qualified name of this class. | `BaseObject` |
| [`hasMethod()`](#method-hasMethod) | Returns a value indicating whether a method is defined. | `BaseObject` |
| [`hasProperty()`](#method-hasProperty) | Returns a value indicating whether a property is defined. | `BaseObject` |
| [`init()`](#method-init) | Initializes the object. | `BaseObject` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(array<string, mixed> $config = [])
```

Constructor.

The default implementation does two things:

- Initializes the object with the given configuration `$config`.
- Call [`init()`](/docs/api/yii/base/baseobject/index.md#method-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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$config` | `array<string, mixed>` | Name-value pairs that will be used to initialize the object properties |

### __call() <span id="method-__call"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The method name |
| `$params` | `array` | Method parameters |

**Returns**

`mixed`: The method return value

**Throws**

- [`UnknownMethodException`](/docs/api/yii/base/unknownmethodexception/index.md): when calling unknown method

### __get() <span id="method-__get"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name |

**Returns**

`mixed`: The property value

**Throws**

- [`UnknownPropertyException`](/docs/api/yii/base/unknownpropertyexception/index.md): if the property is not defined
- [`InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the property is write-only

**See also**

- [`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set)

### __isset() <span id="method-__isset"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name or the event name |

**Returns**

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

**See also**

- <https://www.php.net/manual/en/function.isset.php>

### __set() <span id="method-__set"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name or the event name |
| `$value` | `mixed` | The property value |

**Returns**

`mixed`

**Throws**

- [`UnknownPropertyException`](/docs/api/yii/base/unknownpropertyexception/index.md): if the property is not defined
- [`InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the property is read-only

**See also**

- [`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get)

### __unset() <span id="method-__unset"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The property name |

**Returns**

`mixed`

**Throws**

- [`InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the property is read only.

**See also**

- <https://www.php.net/manual/en/function.unset.php>

### canGetProperty() <span id="method-canGetProperty"></span>

```php
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**

| 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()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty)

### canSetProperty() <span id="method-canSetProperty"></span>

```php
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**

| 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**

- [`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty)

### className() <span id="method-className"></span>

```php
public static function className(): string
```

Returns the fully qualified name of this class.

**Returns**

`string`: The fully qualified name of this class.

### hasMethod() <span id="method-hasMethod"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The method name |

**Returns**

`bool`: Whether the method is defined

### hasProperty() <span id="method-hasProperty"></span>

```php
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**

| 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**

- [`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty)
- [`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty)

### init() <span id="method-init"></span>

```php
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`