Class yii\base\BaseObject

继承yii\base\BaseObject
实现yii\base\Configurable
子类yii\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\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\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
可用自版本2.0.13
源代码 https://github.com/yiisoft/yii2/blob/master/framework/base/BaseObject.php

BaseObject 是实现 属性(property) 特性的基类.

属性由getter方法(eg. getLabel)和/或setter方法(eg. setLabel)定义. 例如, 以下gettersetter方法定义了一个名为label的属性:

private $_label;

public function getLabel()
{
    return $this->_label;
}

public function setLabel($value)
{
    $this->_label = $value;
}

属性名称 不区分大小写.

属性可以像对象的成员变量一样访问. 读取或写入属性将导致调用相应的gettersetter方法. 例如:

// 相当于 $label = $object->getLabel();
$label = $object->label;
// 相当于 $object->setLabel('abc');
$object->label = 'abc';

如果一个属性只有getter方法且没有setter方法, 则将其视为只读. 在这种情况下, 尝试修改属性值将导致异常.

可以调用 hasProperty(), canGetProperty() 和/或 canSetProperty() 来检查属性是否存在.

除了属性特性之外, BaseObject 还引入了一个重要的对象初始化生命周期. 特别地, 创建 BaseObject 或其派生类的新实例将依次涉及以下生命周期:

  1. 调用类构造函数;
  2. 根据给定的配置初始化对象属性;
  3. 调用init()方法.

在上面, 步骤2和3都发生在类构造函数的末尾. 建议你在init()方法中执行对象初始化, 因为在该阶段已经应用了对象配置.

为了确保上述生命周期, 如果 BaseObject 的子类需要重写构造函数, 则应该像下面这样完成:

public function __construct($param1, $param2, ..., $config = [])
{
    ...
    parent::__construct($config);
}

也就是说, $config参数(默认为[])应该被声明为构造函数的最后一个参数, 并且在构造函数的末尾调用父实现.

公共方法

隐藏继承的方法

方法描述定义在
__call() 调用不是类方法的已命名方法. yii\base\BaseObject
__construct() 构造函数. yii\base\BaseObject
__get() 返回对象属性的值. yii\base\BaseObject
__isset() 检查属性是否已设置, 即已定义且不为null. yii\base\BaseObject
__set() 设置对象属性的值. yii\base\BaseObject
__unset() 将对象属性设置为null. yii\base\BaseObject
canGetProperty() 返回一个值, 指示属性是否可读. yii\base\BaseObject
canSetProperty() 返回一个值, 指示属性是否可写. yii\base\BaseObject
className() 返回此类的完全限定名称. yii\base\BaseObject
hasMethod() 返回一个值, 指示方法是否已定义. yii\base\BaseObject
hasProperty() 返回一个值, 指示属性是否已定义. yii\base\BaseObject
init() 初始化对象. yii\base\BaseObject

方法详情

__call() public 方法

调用不是类方法的已命名方法.

不要直接调用此方法, 因为它是一种 PHP 魔术方法, 在调用一个未知方法时将被隐式的调用.

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

方法的名称.

$params array

方法的参数.

return mixed

方法的返回值.

throws yii\base\UnknownMethodException

当调用未知方法时.

__construct() public 方法

构造函数.

默认实现有两件事:

  • 使用给定的配置$config初始化对象.
  • 调用 init().

如果在子类中重写此方法, 则建议使用这种方法:

  • 构造函数的最后一个参数是一个配置数组, 如$config.
  • 在构造函数的末尾调用父实现.
public void __construct ( $config = [] )
$config array

将用于初始化对象属性的键值对(name-value).

__get() public 方法

返回对象属性的值.

不要直接调用此方法, 因为它是一个 PHP 魔术方法, 在执行$value = $object->property;时会被隐式调用.

参见 __set().

public mixed __get ( $name )
$name string

属性名称.

return mixed

属性的值.

throws yii\base\UnknownPropertyException

如果该属性未定义.

throws yii\base\InvalidCallException

如果该属性是只写的.

__isset() public 方法

检查属性是否已设置, 即已定义且不为null.

不要直接调用此方法, 因为它是一个 PHP 魔术方法, 在执行isset($object->property)时会被隐式调用.

Note: 如果未定义属性, 则会返回false.

参见 https://secure.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

属性名称或事件名称.

return boolean

是否设置了已命名属性(不为null).

__set() public 方法

设置对象属性的值.

不要直接调用此方法, 因为它是一个 PHP 魔术方法, 在执行$object->property = $value;时会被隐式调用.

参见 __get().

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

属性名称或事件名称.

$value mixed

属性的值.

throws yii\base\UnknownPropertyException

如果该属性未定义.

throws yii\base\InvalidCallException

如果该属性是只读的.

__unset() public 方法

将对象属性设置为null.

不要直接调用此方法, 因为它是一个 PHP 魔术方法, 在执行unset($object->property)时会被隐式调用.

Note: 如果未定义属性, 则此方法将不执行任何操作. 如果属性是只读的, 则会抛出异常.

参见 https://secure.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

属性名称

throws yii\base\InvalidCallException

如果该属性是只读的.

canGetProperty() public 方法

返回一个值, 指示属性是否可读.

在以下情况下, 属性是可读的:

  • 该类具有与指定名称相关联的getter方法(在这种情况下, 属性名称不区分大小写);
  • 该类有一个具有指定名称的成员变量(当$checkVarstrue时);

参见 canSetProperty().

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

属性的名称.

$checkVars boolean

是否将成员变量视为属性.

return boolean

该属性是否可读.

canSetProperty() public 方法

返回一个值, 指示属性是否可写.

在以下情况下, 属性是可写的:

  • 该类具有与指定名称相关联的setter方法(在这种情况下, 属性名称不区分大小写);
  • 该类有一个具有指定名称的成员变量(当$checkVarstrue时);

参见 canGetProperty().

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

属性的名称.

$checkVars boolean

是否将成员变量视为属性.

return boolean

该属性是否可写.

className() public static 方法
弃用 自 2.0.14. 在 PHP >=5.5 上, 改用::class.

返回此类的完全限定名称.

public static string className ( )
return string

该类的完全限定名称.

hasMethod() public 方法

返回一个值, 指示方法是否已定义.

默认的实现是调用 PHP 函数method_exists(). 当你实现 PHP 魔术方法__call()时, 你可以覆盖此方法.

public boolean hasMethod ( $name )
$name string

方法的名称.

return boolean

方法是否已定义.

hasProperty() public 方法

返回一个值, 指示属性是否已定义.

在以下情况下, 属性是已定义的:

  • 该类具有与指定名称相关联的gettersetter方法(在这种情况下, 属性名称不区分大小写);
  • 该类有一个具有指定名称的成员变量(当$checkVarstrue时);

参见:

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

属性的名称.

$checkVars boolean

是否将成员变量视为属性.

return boolean

属性是否已定义.

init() public 方法

初始化对象.

在使用给定的配置初始化对象之后, 在构造函数的末尾调用此方法.

public void init ( )