Commit 94326571 by Larry Ullman

Initial edits

parent b6630078
Configurations Configurations
============== ==============
Configurations are widely used in Yii for creating new objects or initializing existing objects. Configurations are widely used in Yii when creating new objects or initializing existing objects. Configurations usually include the class name of the object being created, and a list of initial values
They usually include the class names of the objects being created and a list of initial values that should be assigned to the object's [properties](concept-properties.md). Configurations may also include a list of
that should be assigned to object [properties](concept-properties.md). They may also include a list of handlers that should be attached to the object's [events](concept-events.md) and/or a list of
handlers that should be attached to the object [events](concept-events.md), and/or a list of [behaviors](concept-behaviors.md) that should also be attached to the object.
[behaviors](concept-behaviors.md) that should be attached to the objects.
In the following, a configuration is used to create and initialize a DB connection: In the following, a configuration is used to create and initialize a database connection:
```php ```php
$config = [ $config = [
...@@ -21,23 +20,22 @@ $config = [ ...@@ -21,23 +20,22 @@ $config = [
$db = Yii::createObject($config); $db = Yii::createObject($config);
``` ```
The [[Yii::createObject()]] method takes a configuration and creates an object based on the class name The [[Yii::createObject()]] method takes a configuration array as its argument, and creates an object by instantiating the class named in the configuration. When the object is instantiated, the rest of the configuration
specified in the configuration. When the object is being instantiated, the rest of the configuration will be used to initialize the object's properties, event handlers, and behaviors.
will be used to initialize the object properties, event handlers and/or behaviors.
If you already have an object, you may use [[Yii::configure()]] to initialize the object properties with If you already have an object, you may use [[Yii::configure()]] to initialize the object's properties with
a configuration, like the following, a configuration array:
```php ```php
Yii::configure($object, $config); Yii::configure($object, $config);
``` ```
Note that in this case, the configuration should not contain the `class` element. Note that, in this case, the configuration array should not contain a `class` element.
## Configuration Format <a name="configuration-format"></a> ## Configuration Format <a name="configuration-format"></a>
The format of a configuration can be formally described as follows, The format of a configuration can be formally described as:
```php ```php
[ [
...@@ -51,17 +49,17 @@ The format of a configuration can be formally described as follows, ...@@ -51,17 +49,17 @@ The format of a configuration can be formally described as follows,
where where
* The `class` element specifies a fully qualified class name for the object being created. * The `class` element specifies a fully qualified class name for the object being created.
* The `propertyName` elements specify the property initial values. The keys are the property names, and the * The `propertyName` elements specify the initial values for the named property. The keys are the property names, and the
values are the corresponding initial values. Only public member variables and [properties](concept-properties.md) values are the corresponding initial values. Only public member variables and [properties](concept-properties.md)
defined by getters/setters can be configured. defined by getters/setters can be configured.
* The `on eventName` elements specify what handlers should be attached to the object [events](concept-events.md). * The `on eventName` elements specify what handlers should be attached to the object's [events](concept-events.md).
Notice that the array keys are formed by prefixing event names with `on `. Please refer to Notice that the array keys are formed by prefixing event names with `on `. Please refer to
the [Events](concept-events.md) section for supported event handler formats. the [Events](concept-events.md) section for supported event handler formats.
* And the `as behaviorName` elements specify what [behaviors](concept-behaviors.md) should be attached to the object. * The `as behaviorName` elements specify what [behaviors](concept-behaviors.md) should be attached to the object.
Notice that the array keys are formed by prefixing behavior names with `as `. `$behaviorConfig` represents Notice that the array keys are formed by prefixing behavior names with `as `; the value, `$behaviorConfig`, represents
the configuration for creating a behavior, like a normal configuration as we are describing here. the configuration for creating a behavior, like a normal configuration described here.
Below is an example showing a configuration with property initial values, event handlers and behaviors: Below is an example showing a configuration with initial property values, event handlers, and behaviors:
```php ```php
[ [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment