# Expression

> Expression represents a DB expression that does not need escaping or quoting.

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

| class | `yii\db\Expression` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/db/Expression.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/Expression.php#L32) |

Expression represents a DB expression that does not need escaping or quoting.

When an Expression object is embedded within a SQL statement or fragment,
it will be replaced with the [`$expression`](/docs/api/yii/db/expression/index.md#property-expression) property value without any
DB escaping or quoting. For example,

```
$expression = new Expression('NOW()');
$now = (new \yii\db\Query)->select($expression)->scalar();  // SELECT NOW();
echo $now; // prints the current date
```

Expression objects are mainly created for passing raw SQL expressions to methods of
[`Query`](/docs/api/yii/db/query/index.md), [`ActiveQuery`](/docs/api/yii/db/activequery/index.md), and related classes.

An expression can also be bound with parameters specified via [`$params`](/docs/api/yii/db/expression/index.md#property-params).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-expression"></span>`$expression` | `string` | The DB expression Default: `null`. | `Expression` |
| [`$params`](#property-params) | `array` | List of parameters that should be bound for this expression. Default: `[]`. | `Expression` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Constructor. | `Expression` |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`__toString()`](#method-__toString) | String magic method. | `Expression` |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |

## Property details

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

```php
public array $params = []
```

List of parameters that should be bound for this expression.

The keys are placeholders appearing in [`$expression`](/docs/api/yii/db/expression/index.md#property-expression) and the values
are the corresponding parameter values.

## Method details

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

```php
public function __construct(string $expression, array $params = [], array $config = [])
```

Constructor.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$expression` | `string` | The DB expression |
| `$params` | `array` | Parameters |
| `$config` | `array` | Name-value pairs that will be used to initialize the object properties |

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

```php
public function __toString(): string
```

String magic method.

**Returns**

`string`: The DB expression.