0 follower

Class yii\base\BaseObject

Inheritanceyii\base\BaseObject
Implementsyii\base\Configurable
Subclassesyii\base\Action, yii\base\ActionEvent, yii\base\ActionFilter, yii\base\Application, yii\base\Behavior, yii\base\Component, yii\base\Controller, yii\base\DynamicModel, yii\base\ErrorHandler, yii\base\Event, yii\base\InlineAction, yii\base\Model, yii\base\ModelEvent, yii\base\Module, yii\base\Object, yii\base\Request, yii\base\Response, yii\base\Security, yii\base\Theme, yii\base\View, yii\base\ViewEvent, yii\base\ViewRenderer, yii\base\Widget, yii\base\WidgetEvent, yii\behaviors\AttributeBehavior, yii\behaviors\AttributeTypecastBehavior, yii\behaviors\AttributesBehavior, yii\behaviors\BlameableBehavior, yii\behaviors\CacheableWidgetBehavior, yii\behaviors\OptimisticLockBehavior, yii\behaviors\SluggableBehavior, yii\behaviors\TimestampBehavior, yii\caching\ApcCache, yii\caching\ArrayCache, yii\caching\Cache, yii\caching\ChainedDependency, yii\caching\DbCache, yii\caching\DbDependency, yii\caching\DbQueryDependency, yii\caching\Dependency, yii\caching\DummyCache, yii\caching\ExpressionDependency, yii\caching\FileCache, yii\caching\FileDependency, yii\caching\MemCache, yii\caching\MemCacheServer, yii\caching\TagDependency, yii\caching\WinCache, yii\caching\XCache, yii\caching\ZendDataCache, yii\captcha\Captcha, yii\captcha\CaptchaAction, yii\captcha\CaptchaAsset, yii\captcha\CaptchaValidator, yii\console\Application, yii\console\Controller, yii\console\ErrorHandler, yii\console\Request, yii\console\Response, yii\console\controllers\AssetController, yii\console\controllers\BaseMigrateController, yii\console\controllers\CacheController, yii\console\controllers\FixtureController, yii\console\controllers\HelpController, yii\console\controllers\MessageController, yii\console\controllers\MigrateController, yii\console\controllers\ServeController, yii\console\widgets\Table, yii\data\ActiveDataFilter, yii\data\ActiveDataProvider, yii\data\ArrayDataProvider, yii\data\BaseDataProvider, yii\data\DataFilter, yii\data\Pagination, yii\data\Sort, yii\data\SqlDataProvider, yii\db\ActiveQuery, yii\db\ActiveRecord, yii\db\AfterSaveEvent, yii\db\BaseActiveRecord, yii\db\BatchQueryResult, yii\db\CheckConstraint, yii\db\ColumnSchema, yii\db\ColumnSchemaBuilder, yii\db\Command, yii\db\Connection, yii\db\Constraint, yii\db\DataReader, yii\db\DefaultValueConstraint, yii\db\Expression, yii\db\ForeignKeyConstraint, yii\db\IndexConstraint, yii\db\Migration, yii\db\Query, yii\db\QueryBuilder, yii\db\Schema, yii\db\SqlToken, yii\db\SqlTokenizer, yii\db\TableSchema, yii\db\Transaction, yii\db\cubrid\ColumnSchemaBuilder, yii\db\cubrid\QueryBuilder, yii\db\cubrid\Schema, yii\db\mssql\ColumnSchema, yii\db\mssql\ColumnSchemaBuilder, yii\db\mssql\QueryBuilder, yii\db\mssql\Schema, yii\db\mssql\TableSchema, yii\db\mysql\ColumnSchema, yii\db\mysql\ColumnSchemaBuilder, yii\db\mysql\QueryBuilder, yii\db\mysql\Schema, yii\db\oci\ColumnSchemaBuilder, yii\db\oci\Command, yii\db\oci\QueryBuilder, yii\db\oci\Schema, yii\db\pgsql\ColumnSchema, yii\db\pgsql\QueryBuilder, yii\db\pgsql\Schema, yii\db\sqlite\ColumnSchemaBuilder, yii\db\sqlite\Command, yii\db\sqlite\QueryBuilder, yii\db\sqlite\Schema, yii\db\sqlite\SqlTokenizer, yii\di\Container, yii\di\ServiceLocator, yii\filters\AccessControl, yii\filters\AccessRule, yii\filters\AjaxFilter, yii\filters\ContentNegotiator, yii\filters\Cors, yii\filters\HostControl, yii\filters\HttpCache, yii\filters\PageCache, yii\filters\RateLimiter, yii\filters\VerbFilter, yii\filters\auth\AuthMethod, yii\filters\auth\CompositeAuth, yii\filters\auth\HttpBasicAuth, yii\filters\auth\HttpBearerAuth, yii\filters\auth\HttpHeaderAuth, yii\filters\auth\QueryParamAuth, yii\grid\ActionColumn, yii\grid\CheckboxColumn, yii\grid\Column, yii\grid\DataColumn, yii\grid\GridView, yii\grid\GridViewAsset, yii\grid\RadioButtonColumn, yii\grid\SerialColumn, yii\i18n\DbMessageSource, yii\i18n\Formatter, yii\i18n\GettextFile, yii\i18n\GettextMessageSource, yii\i18n\GettextMoFile, yii\i18n\GettextPoFile, yii\i18n\I18N, yii\i18n\Locale, yii\i18n\MessageFormatter, yii\i18n\MessageSource, yii\i18n\MissingTranslationEvent, yii\i18n\PhpMessageSource, yii\log\DbTarget, yii\log\Dispatcher, yii\log\EmailTarget, yii\log\FileTarget, yii\log\Logger, yii\log\SyslogTarget, yii\log\Target, yii\mail\BaseMailer, yii\mail\BaseMessage, yii\mail\MailEvent, yii\mutex\DbMutex, yii\mutex\FileMutex, yii\mutex\Mutex, yii\mutex\MysqlMutex, yii\mutex\OracleMutex, yii\mutex\PgsqlMutex, yii\rbac\Assignment, yii\rbac\BaseManager, yii\rbac\DbManager, yii\rbac\Item, yii\rbac\Permission, yii\rbac\PhpManager, yii\rbac\Role, yii\rbac\Rule, yii\rest\Action, yii\rest\ActiveController, yii\rest\Controller, yii\rest\CreateAction, yii\rest\DeleteAction, yii\rest\IndexAction, yii\rest\OptionsAction, yii\rest\Serializer, yii\rest\UpdateAction, yii\rest\UrlRule, yii\rest\ViewAction, yii\test\ActiveFixture, yii\test\ArrayFixture, yii\test\BaseActiveFixture, yii\test\DbFixture, yii\test\Fixture, yii\test\InitDbFixture, yii\validators\BooleanValidator, yii\validators\CompareValidator, yii\validators\DateValidator, yii\validators\DefaultValueValidator, yii\validators\EachValidator, yii\validators\EmailValidator, yii\validators\ExistValidator, yii\validators\FileValidator, yii\validators\FilterValidator, yii\validators\ImageValidator, yii\validators\InlineValidator, yii\validators\IpValidator, yii\validators\NumberValidator, yii\validators\PunycodeAsset, yii\validators\RangeValidator, yii\validators\RegularExpressionValidator, yii\validators\RequiredValidator, yii\validators\SafeValidator, yii\validators\StringValidator, yii\validators\TrimValidator, yii\validators\UniqueValidator, yii\validators\UrlValidator, yii\validators\ValidationAsset, yii\validators\Validator, yii\web\Application, yii\web\AssetBundle, yii\web\AssetConverter, yii\web\AssetManager, yii\web\CacheSession, yii\web\CompositeUrlRule, yii\web\Controller, yii\web\Cookie, yii\web\CookieCollection, yii\web\DbSession, yii\web\ErrorAction, yii\web\ErrorHandler, yii\web\GroupUrlRule, yii\web\HeaderCollection, yii\web\HtmlResponseFormatter, yii\web\JqueryAsset, yii\web\JsExpression, yii\web\JsonResponseFormatter, yii\web\Link, yii\web\MultiFieldSession, yii\web\MultipartFormDataParser, yii\web\Request, yii\web\Response, yii\web\Session, yii\web\UploadedFile, yii\web\UrlManager, yii\web\UrlNormalizer, yii\web\UrlRule, yii\web\User, yii\web\UserEvent, yii\web\View, yii\web\ViewAction, yii\web\XmlResponseFormatter, yii\web\YiiAsset, yii\widgets\ActiveField, yii\widgets\ActiveForm, yii\widgets\ActiveFormAsset, yii\widgets\BaseListView, yii\widgets\Block, yii\widgets\Breadcrumbs, yii\widgets\ContentDecorator, yii\widgets\DetailView, yii\widgets\FragmentCache, yii\widgets\InputWidget, yii\widgets\LinkPager, yii\widgets\LinkSorter, yii\widgets\ListView, yii\widgets\MaskedInput, yii\widgets\MaskedInputAsset, yii\widgets\Menu, yii\widgets\Pjax, yii\widgets\PjaxAsset, yii\widgets\Spaceless
Available since version2.0.13
Source Code https://github.com/yiisoft/yii2/blob/master/framework/base/BaseObject.php

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.

Public Methods

Hide inherited methods

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

Method Details

Hide inherited methods

__call() public method

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.

public mixed __call ( $name, $params )
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

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.
public void __construct ( $config = [] )
$config array

Name-value pairs that will be used to initialize the object properties

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() public method

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

See also __set().

public mixed __get ( $name )
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is write-only

                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);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

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.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

The property name or the event name

return boolean

Whether the named property is set (not null).

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

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

See also __get().

public void __set ( $name, $value )
$name string

The property name or the event name

$value mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is read-only

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

            
__unset() public method

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.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

The property name

throws yii\base\InvalidCallException

if the property is read only.

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

            
canGetProperty() public method

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

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be read

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

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

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be written

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

Returns the fully qualified name of this class.

public static string className ( )
return string

The fully qualified name of this class.

                public static function className()
{
    return get_called_class();
}

            
hasMethod() public method

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

public boolean hasMethod ( $name )
$name string

The method name

return boolean

Whether the method is defined

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

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

See also:

public boolean hasProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property is defined

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

Initializes the object.

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

public void init ( )

                public function init()
{
}