Skip to content
PHP Ecosystem
Explore

BaseYii

API reference › yii › BaseYii

classyii\BaseYii
Since2.0
Sourceframework/BaseYii.php
1 subclass

BaseYii is the core helper class for the Yii framework.

Do not use BaseYii directly. Instead, use its child class Yii which you can replace to customize methods of BaseYii.

PropertyTypeDescriptionDefined by
$aliases staticarrayRegistered path aliases Default: ['@yii' => __DIR__].BaseYii
$app staticyii\console\Application|yii\web\Application<yii\TUserIdentity>|nullThe application instance Default: null.BaseYii
$container staticyii\di\ContainerThe dependency injection (DI) container used by createObject(). Default: null.BaseYii
MethodDescriptionDefined by
beginProfile() staticMarks the beginning of a code block for profiling.BaseYii
configure() staticConfigures an object with the initial property values.BaseYii
createObject() staticCreates a new object using the given configuration.BaseYii
debug() staticLogs a debug message.BaseYii
endProfile() staticMarks the end of a code block for profiling.BaseYii
error() staticLogs an error message.BaseYii
getAlias() staticTranslates a path alias into an actual path.BaseYii
getLogger() staticBaseYii
getObjectVars() staticReturns the public member variables of an object.BaseYii
getRootAlias() staticReturns the root alias part of a given alias.BaseYii
getVersion() staticReturns a string representing the current version of the Yii framework.BaseYii
info() staticLogs an informative message.BaseYii
setAlias() staticRegisters a path alias.BaseYii
setLogger() staticSets the logger object.BaseYii
t() staticTranslates a message to the specified language.BaseYii
warning() staticLogs a warning message.BaseYii
public static yii\di\Container $container = null

The dependency injection (DI) container used by createObject().

You may use Container::set() to set up the needed dependencies of classes and their initial property values.

public static function beginProfile(string $token, string $category = 'application'): mixed

Marks the beginning of a code block for profiling.

This has to be matched with a call to endProfile() with the same category name. The begin- and end- calls must also be properly nested. For example,

\Yii::beginProfile('block1');
// some code to be profiled
\Yii::beginProfile('block2');
// some other code to be profiled
\Yii::endProfile('block2');
\Yii::endProfile('block1');

Parameters

ParameterTypeDescription
$tokenstringToken for the code block
$categorystringThe category of this log message

Returns

mixed

See also

public static function configure(object $object, array $properties): object

Configures an object with the initial property values.

Parameters

ParameterTypeDescription
$objectobjectThe object to be configured
$propertiesarrayThe property initial values given in terms of name-value pairs.

Returns

object: The object itself

public static function createObject(class-string<yii\T>|array{class?: class-string<yii\T>, __class?: class-string<yii\T>}|callable(): yii\T $type, array $params = []): yii\T

Creates a new object using the given configuration.

You may view this method as an enhanced version of the new operator. The method supports creating an object based on a class name, a configuration array or an anonymous function.

Below are some usage examples:

// create an object using a class name
$object = Yii::createObject('yii\db\Connection');
// create an object using a configuration array
$object = Yii::createObject([
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
]);
// create an object with two constructor parameters
$object = \Yii::createObject('MyClass', [$param1, $param2]);

Using dependency injection container, this method can also identify dependent objects, instantiate them and inject them into the newly created object.

Parameters

ParameterTypeDescription
$typeclass-string<yii\T>|array{class?: class-string<yii\T>, __class?: class-string<yii\T>}|callable(): yii\TThe object type. This can be specified in one of the following forms: - a string: representing the class name of the object to be created - a configuration array: the array must contain a class element which is treated as the object class, and the rest of the name-value pairs will be used to initialize the corresponding object properties - a PHP callable: either an anonymous function or an array representing a class method ([$class or $object, $method]). The callable should return a new instance of the object being created.
$paramsarrayThe constructor parameters

Returns

yii\T: The created object

Throws

See also

public static function debug(string|array $message, string $category = 'application'): mixed

Logs a debug message.

Trace messages are logged mainly for development purposes to see the execution workflow of some code. This method will only log a message when the application is in debug mode.

Parameters

ParameterTypeDescription
$messagestring|arrayThe message to be logged. This can be a simple string or a more complex data structure, such as an array.
$categorystringThe category of the message.

Returns

mixed

Since

2.0.14

public static function endProfile(string $token, string $category = 'application'): mixed

Marks the end of a code block for profiling.

This has to be matched with a previous call to beginProfile() with the same category name.

Parameters

ParameterTypeDescription
$tokenstringToken for the code block
$categorystringThe category of this log message

Returns

mixed

See also

public static function error(string|array|Throwable $message, string $category = 'application'): mixed

Logs an error message.

An error message is typically logged when an unrecoverable error occurs during the execution of an application.

Parameters

ParameterTypeDescription
$messagestring|array|ThrowableThe message to be logged. This can be a simple string or a more complex data structure, such as an array.
$categorystringThe category of the message.

Returns

mixed

public static function getAlias(string $alias, bool $throwException = true): ($throwException is true ? string : string|false)

Translates a path alias into an actual path.

The translation is done according to the following procedure:

  1. If the given alias does not start with ’@’, it is returned back without change;
  2. Otherwise, look for the longest registered alias that matches the beginning part of the given alias. If it exists, replace the matching part of the given alias with the corresponding registered path.
  3. Throw an exception or return false, depending on the $throwException parameter.

For example, by default ‘@yii’ is registered as the alias to the Yii framework directory, say ‘/path/to/yii’. The alias ‘@yii/web’ would then be translated into ‘/path/to/yii/web’.

If you have registered two aliases ‘@foo’ and ‘@foo/bar’. Then translating ‘@foo/bar/config’ would replace the part ‘@foo/bar’ (instead of ‘@foo’) with the corresponding registered path. This is because the longest alias takes precedence.

However, if the alias to be translated is ‘@foo/barbar/config’, then ‘@foo’ will be replaced instead of ‘@foo/bar’, because ’/’ serves as the boundary character.

Note, this method does not check if the returned path exists or not.

See the guide article on aliases for more information.

Parameters

ParameterTypeDescription
$aliasstringThe alias to be translated.
$throwExceptionboolWhether to throw an exception if the given alias is invalid. If this is false and an invalid alias is given, false will be returned by this method.

Returns

($throwException is true ? string : string|false): The path corresponding to the alias, false if the root alias is not previously registered.

Throws

See also

public static function getLogger(): yii\log\Logger

Returns

yii\log\Logger: Message logger

public static function getObjectVars(object $object): array

Returns the public member variables of an object.

This method is provided such that we can get the public member variables of an object. It is different from “get_object_vars()” because the latter will return private and protected variables if it is called within the object itself.

Parameters

ParameterTypeDescription
$objectobjectThe object to be handled

Returns

array: The public member variables of the object

public static function getRootAlias(string $alias): string|false

Returns the root alias part of a given alias.

A root alias is an alias that has been registered via setAlias() previously. If a given alias matches multiple root aliases, the longest one will be returned.

Parameters

ParameterTypeDescription
$aliasstringThe alias

Returns

string|false: The root alias, or false if no root alias is found

public static function getVersion(): string

Returns a string representing the current version of the Yii framework.

Returns

string: The version of Yii framework

public static function info(string|array|Throwable $message, string $category = 'application'): mixed

Logs an informative message.

An informative message is typically logged by an application to keep record of something important (e.g. an administrator logs in).

Parameters

ParameterTypeDescription
$messagestring|array|ThrowableThe message to be logged. This can be a simple string or a more complex data structure, such as an array.
$categorystringThe category of the message.

Returns

mixed

public static function setAlias(string $alias, string|null $path): mixed

Registers a path alias.

A path alias is a short name representing a long path (a file path, a URL, etc.) For example, we use ‘@yii’ as the alias of the path to the Yii framework directory.

A path alias must start with the character ’@’ so that it can be easily differentiated from non-alias paths.

Note that this method does not check if the given path exists or not. All it does is to associate the alias with the path.

Any trailing ’/’ and ” characters in the given path will be trimmed.

See the guide article on aliases for more information.

Parameters

ParameterTypeDescription
$aliasstringThe alias name (e.g. “@yii”). It must start with a ’@’ character. It may contain the forward-slash ’/’ which serves as a boundary character when performing alias translation by getAlias().
$pathstring|nullThe path corresponding to the alias. If this is null, the alias will be removed. Trailing ’/’ and ” characters will be trimmed. This can be - a directory or a file path (e.g. /tmp, /tmp/main.txt) - a URL (e.g. https://www.yiiframework.com) - a path alias (e.g. @yii/base). In this case, the path alias will be converted into the actual path first by calling getAlias().

Returns

mixed

Throws

See also

public static function setLogger(yii\log\Logger|null $logger): mixed

Sets the logger object.

Parameters

ParameterTypeDescription
$loggeryii\log\Logger|nullThe logger object.

Returns

mixed

public static function t(string $category, string $message, array $params = [], string|null $language = null): string

Translates a message to the specified language.

This is a shortcut method of I18N::translate().

The translation will be conducted according to the message category and the target language will be used.

You can add parameters to a translation message that will be substituted with the corresponding value after translation. The format for this is to use curly brackets around the parameter name as you can see in the following example:

$username = 'Alexander';
echo \Yii::t('app', 'Hello, {username}!', ['username' => $username]);

Further formatting of message parameters is supported using the PHP intl extensions message formatter. See I18N::translate() for more details.

Parameters

ParameterTypeDescription
$categorystringThe message category.
$messagestringThe message to be translated.
$paramsarrayThe parameters that will be used to replace the corresponding placeholders in the message.
$languagestring|nullThe language code (e.g. en-US, en). If this is null, the current application language will be used.

Returns

string: The translated message.

public static function warning(string|array|Throwable $message, string $category = 'application'): mixed

Logs a warning message.

A warning message is typically logged when an error occurs while the execution can still continue.

Parameters

ParameterTypeDescription
$messagestring|array|ThrowableThe message to be logged. This can be a simple string or a more complex data structure, such as an array.
$categorystringThe category of the message.

Returns

mixed