# BaseYii

> BaseYii is the core helper class for the Yii framework.

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

| class | `yii\BaseYii` |
| --- | --- |
| Since | 2.0 |
| Source | [`framework/BaseYii.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/BaseYii.php#L63) |

1 subclass:

- [`Yii`](/docs/api/global/yii/index.md)

BaseYii is the core helper class for the Yii framework.

Do not use BaseYii directly. Instead, use its child class [`Yii`](/docs/api/global/yii/index.md) which you can replace to
customize methods of BaseYii.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-aliases"></span>`$aliases` static | `array` | Registered path aliases Default: `['@yii' => __DIR__]`. | `BaseYii` |
| <span id="property-app"></span>`$app` static | [`yii\console\Application`](/docs/api/yii/console/application/index.md)`\|`[`yii\web\Application`](/docs/api/yii/web/application/index.md)`<yii\TUserIdentity>\|null` | The application instance Default: `null`. | `BaseYii` |
| [`$container`](#property-container) static | [`yii\di\Container`](/docs/api/yii/di/container/index.md) | The dependency injection (DI) container used by [`createObject()`](/docs/api/yii/baseyii/index.md#method-createObject). Default: `null`. | `BaseYii` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`beginProfile()`](#method-beginProfile) static | Marks the beginning of a code block for profiling. | `BaseYii` |
| [`configure()`](#method-configure) static | Configures an object with the initial property values. | `BaseYii` |
| [`createObject()`](#method-createObject) static | Creates a new object using the given configuration. | `BaseYii` |
| [`debug()`](#method-debug) static | Logs a debug message. | `BaseYii` |
| [`endProfile()`](#method-endProfile) static | Marks the end of a code block for profiling. | `BaseYii` |
| [`error()`](#method-error) static | Logs an error message. | `BaseYii` |
| [`getAlias()`](#method-getAlias) static | Translates a path alias into an actual path. | `BaseYii` |
| [`getLogger()`](#method-getLogger) static |  | `BaseYii` |
| [`getObjectVars()`](#method-getObjectVars) static | Returns the public member variables of an object. | `BaseYii` |
| [`getRootAlias()`](#method-getRootAlias) static | Returns the root alias part of a given alias. | `BaseYii` |
| [`getVersion()`](#method-getVersion) static | Returns a string representing the current version of the Yii framework. | `BaseYii` |
| [`info()`](#method-info) static | Logs an informative message. | `BaseYii` |
| [`setAlias()`](#method-setAlias) static | Registers a path alias. | `BaseYii` |
| [`setLogger()`](#method-setLogger) static | Sets the logger object. | `BaseYii` |
| [`t()`](#method-t) static | Translates a message to the specified language. | `BaseYii` |
| [`warning()`](#method-warning) static | Logs a warning message. | `BaseYii` |

## Property details

### $container <span id="property-container"></span>

```php
public static yii\di\Container $container = null
```

The dependency injection (DI) container used by [`createObject()`](/docs/api/yii/baseyii/index.md#method-createObject).

You may use [`Container::set()`](/docs/api/yii/di/container/index.md#method-set) to set up the needed dependencies of classes and
their initial property values.

## Method details

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

```php
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()`](/docs/api/yii/baseyii/index.md#method-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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$token` | `string` | Token for the code block |
| `$category` | `string` | The category of this log message |

**Returns**

`mixed`

**See also**

- [`endProfile()`](/docs/api/yii/baseyii/index.md#method-endProfile)

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

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

Configures an object with the initial property values.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$object` | `object` | The object to be configured |
| `$properties` | `array` | The property initial values given in terms of name-value pairs. |

**Returns**

`object`: The object itself

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

```php
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](/docs/api/yii/di/container/index.md), this method can also identify
dependent objects, instantiate them and inject them into the newly created object.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$type` | `class-string<yii\T>\|array{class?: class-string<yii\T>, __class?: class-string<yii\T>}\|callable(): yii\T` | The 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. |
| `$params` | `array` | The constructor parameters |

**Returns**

`yii\T`: The created object

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the configuration is invalid.

**See also**

- [`Container`](/docs/api/yii/di/container/index.md)

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

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

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | `string\|array` | The message to be logged. This can be a simple string or a more complex data structure, such as an array. |
| `$category` | `string` | The category of the message. |

**Returns**

`mixed`

**Since**

2.0.14

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

```php
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()`](/docs/api/yii/baseyii/index.md#method-beginProfile) with the same category name.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$token` | `string` | Token for the code block |
| `$category` | `string` | The category of this log message |

**Returns**

`mixed`

**See also**

- [`beginProfile()`](/docs/api/yii/baseyii/index.md#method-beginProfile)

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

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

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | `string\|array\|`[`Throwable`](https://www.php.net/manual/en/class.throwable.php) | The message to be logged. This can be a simple string or a more complex data structure, such as an array. |
| `$category` | `string` | The category of the message. |

**Returns**

`mixed`

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

```php
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](/docs/guide/concept-aliases/index.md) for more information.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$alias` | `string` | The alias to be translated. |
| `$throwException` | `bool` | Whether 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**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if the alias is invalid while $throwException is true.

**See also**

- [`setAlias()`](/docs/api/yii/baseyii/index.md#method-setAlias)

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

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

**Returns**

[`yii\log\Logger`](/docs/api/yii/log/logger/index.md): Message logger

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

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

| Parameter | Type | Description |
| --- | --- | --- |
| `$object` | `object` | The object to be handled |

**Returns**

`array`: The public member variables of the object

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

```php
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()`](/docs/api/yii/baseyii/index.md#method-setAlias) previously.
If a given alias matches multiple root aliases, the longest one will be returned.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$alias` | `string` | The alias |

**Returns**

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

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

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

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

**Returns**

`string`: The version of Yii framework

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

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

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | `string\|array\|`[`Throwable`](https://www.php.net/manual/en/class.throwable.php) | The message to be logged. This can be a simple string or a more complex data structure, such as an array. |
| `$category` | `string` | The category of the message. |

**Returns**

`mixed`

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

```php
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](/docs/guide/concept-aliases/index.md) for more information.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$alias` | `string` | The 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()`](/docs/api/yii/baseyii/index.md#method-getAlias). |
| `$path` | `string\|null` | The 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()`](/docs/api/yii/baseyii/index.md#method-getAlias). |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if $path is an invalid alias.

**See also**

- [`getAlias()`](/docs/api/yii/baseyii/index.md#method-getAlias)

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

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

Sets the logger object.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$logger` | [`yii\log\Logger`](/docs/api/yii/log/logger/index.md)`\|null` | The logger object. |

**Returns**

`mixed`

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

```php
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()`](/docs/api/yii/i18n/i18n/index.md#method-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](https://www.php.net/manual/en/intro.intl.php)
message formatter. See [`I18N::translate()`](/docs/api/yii/i18n/i18n/index.md#method-translate) for more details.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$category` | `string` | The message category. |
| `$message` | `string` | The message to be translated. |
| `$params` | `array` | The parameters that will be used to replace the corresponding placeholders in the message. |
| `$language` | `string\|null` | The language code (e.g. `en-US`, `en`). If this is null, the current [application language](/docs/api/yii/base/application/index.md#property-language) will be used. |

**Returns**

`string`: The translated message.

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

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

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | `string\|array\|`[`Throwable`](https://www.php.net/manual/en/class.throwable.php) | The message to be logged. This can be a simple string or a more complex data structure, such as an array. |
| `$category` | `string` | The category of the message. |

**Returns**

`mixed`