Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Yii

You're reading from   Mastering Yii Advance your modern web application development skills with Yii Framework 2

Arrow left icon
Product type Paperback
Published in Jan 2016
Publisher Packt
ISBN-13 9781785882425
Length 380 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Charles R. Portwood ll Charles R. Portwood ll
Author Profile Icon Charles R. Portwood ll
Charles R. Portwood ll
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Composer, Configuration, Classes, and Path Aliases FREE CHAPTER 2. Console Commands and Applications 3. Migrations, DAO, and Query Building 4. Active Record, Models, and Forms 5. Modules, Widgets, and Helpers 6. Asset Management 7. Authenticating and Authorizing Users 8. Routing, Responses, and Events 9. RESTful APIs 10. Testing with Codeception 11. Internationalization and Localization 12. Performance and Security 13. Debugging and Deploying Index

Components and objects

There are two base classes that almost everything in Yii2 extends from: the Component class and the Object class.

Components

In Yii2, the Component class has replaced the CComponent class from Yii1. In Yii1, components act as service locators that host a specific set of application components that provide different services for the processing of requests. Each component in Yii2 can be accessed using the following syntax:

Yii::$app->componentID

For example, the database component can be accessed using this:

Yii::$app->db

The cache component can be accessed using this:

Yii::$app->cache

Yii2 automatically registers each component at runtime via the application configuration that we mentioned in the previous section by name.

To improve performance in Yii2 applications, components are lazy-loaded or only instantiated the first time they are accessed. This means that if the cache component is never used in your application code, the cache component will never be loaded. At times, however, this can be nonideal, so to force load a component, you can bootstrap it by adding it to the bootstrap configuration option in either config/web.php or config/console.php. For instance, if we want to bootstrap the log component, we can do that as follows:

<?php return [
    'bootstrap' => [
        'log'
    ],
    […]
]

The bootstrap option behaves in a manner similar to the preload option in Yii1—any component that you want or need to be instantiated on bootstrap will be loaded if it is in the bootstrap section of your configuration file.

Note

For more information on service locators and components, ensure that you read the Definitive Guide to Yii guide located at http://www.yiiframework.com/doc-2.0/guide-concept-service-locator.html and http://www.yiiframework.com/doc-2.0/guide-structure-application-components.html.

Objects

In Yii2, almost every class that doesn't extend from the Component class extends from the Object class. The Object class is the base class that implements the property feature. In Yii2, the property feature allows you to access a lot of information about an object, such as the __get and __set magic methods, as well as other utility functions, such as hasProperty(), canGetProperty(), and canSetProperty(). Combined, this makes objects in Yii2 extremely powerful.

Tip

The object class is extremely powerful, and many classes in Yii extend from it. Despite this, using the magic methods __get and __set yourself is not considered best practice as it is slower than a native PHP method and doesn't integrate well with your IDE's autocomplete tool and documentation tools.

You have been reading a chapter from
Mastering Yii
Published in: Jan 2016
Publisher: Packt
ISBN-13: 9781785882425
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image