Content of the «Key Concepts» section:
- Class Autoloading
- Service Locator
- Dependency Injection Container
Articles section «Key Concepts»:
Dependency Injection Container
A dependency injection (DI) container is an object that knows how to instantiate and configure objects and all their dependent objects. Martin Fowler's article has well explained why DI container is useful. Here we will mainly explain the usage of the DI container provided by Yii.
A service locator is an object that knows how to provide all sorts of services (or components) that an application might need. Within a service locator, each component exists as only a single instance, uniquely identified by an ID. You use the ID to retrieve a component from the service locator.
In Yii, a service locator is simply an instance of [[yii\di\ServiceLocator]] or a child class.
Yii relies on the class autoloading mechanism to locate and include all required class files. It provides a high-performance class autoloader that is compliant with the PSR-4 standard. The autoloader is installed when you include the
Aliases are used to represent file paths or URLs so that you don't have to hard-code absolute paths or URLs in your project. An alias must start with the
@ character to be differentiated from normal file paths and URLs. Alias defined without leading
@ will be prefixed with
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 that should be assigned to the object's properties. Configurations may also include a list of handlers that should be attached to the object's events and/or a list of behaviors that should also be attached to the object.
Behaviors are instances of [[yii\base\Behavior]], or of a child class. Behaviors, also known as mixins, allow you to enhance the functionality of an existing [[yii\base\Component|component]] class without needing to change the class's inheritance. Attaching a behavior to a component "injects" the behavior's methods and properties into the component, making those methods and properties accessible as if they were defined in the component class itself. Moreover, a behavior can respond to the events triggered by the component, which allows behaviors to also customize the normal code execution of the component.
Events allow you to inject custom code into existing code at certain execution points. You can attach custom code to an event so that when the event is triggered, the code gets executed automatically. For example, a mailer object may trigger a
messageSent event when it successfully sends a message. If you want to keep track of the messages that are successfully sent, you could then simply attach the tracking code to the
In PHP, class member variables are also called properties. These variables are part of the class definition, and are used to represent the state of a class instance (i.e., to differentiate one instance of the class from another). In practice, you may often want to handle the reading or writing of properties in special ways. For example, you may want to always trim a string when it is being assigned to a
label property. You could use the following code to achieve this task:
$object->label = trim($label);
Components are the main building blocks of Yii applications. Components are instances of [[yii\base\Component]], or an extended class. The three main features that components provide to other classes are: