Home Programming Yii Application Development Cookbook - Second Edition

Yii Application Development Cookbook - Second Edition

By Alexander Makarov , Yii
books-svg-icon Book
Subscription FREE
eBook + Subscription €14.99
eBook €28.99
Print + eBook €37.99
READ FOR FREE Free Trial for 7 days. €14.99 p/m after trial. Cancel Anytime! BUY NOW BUY NOW BUY NOW
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
READ FOR FREE Free Trial for 7 days. €14.99 p/m after trial. Cancel Anytime! BUY NOW BUY NOW BUY NOW
Subscription FREE
eBook + Subscription €14.99
eBook €28.99
Print + eBook €37.99
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
  1. Free Chapter
    Under the Hood
About this book
The Yii framework is a rapidly growing PHP5 MVC framework often referred to as Rails for PHP. It has already become a solid base for many exciting web applications such as Stay.com and can be a good base for your developments, too. This book will help you to learn Yii quickly and in more depth for use in for your developments."Yii Application Development Cookbook" will show you how to use Yii efficiently. You will learn about taking shortcuts using core features, creating your own reusable code base, using test driven development, and many more topics that will give you a lot of experience in a moderate amount of time.The second edition fixes all errata found in the first edition and also features new recipes on the client side, HTTP caching, and using Composer with Yii.The chapters of the book are generally independent and since this book's goal is to enhance a practical approach to Yii development, you can start reading from the chapter you need most, be it Ajax and jQuery, Database, Active Record, and Model Tricks, or Extending Yii."Yii Application Development Cookbook" will help you to learn more about the Yii framework and application development practices in general, showing shortcuts and dangerous things you shouldn't do.With all the recipes grouped in 13 chapters, you will write your applications more efficiently using shortcuts and using Yii core functionality in a good way. The most interesting topics are; Yii application deployment, a guide to writing your own extensions, advanced error handling, debugging and logging, application security, performance tuning, and much more."Yii Application Development Cookbook" will help you to learn more about the Yii framework and application development practices in general. You will write your applications more efficiently using shortcuts and using Yii core functionality in a good way.
Publication date:
April 2013
Publisher
Packt
Pages
408
ISBN
9781782163107

 

Chapter 1. Under the Hood

In this chapter, we will cover:

  • Using getters and setters

  • Using Yii events

  • Using import and autoloading

  • Using exceptions

  • Configuring components

  • Configuring widget defaults

  • Using Yii core collections

  • Working with requests

 

Introduction


In this chapter, we will cover the most interesting Yii features that are hidden "under the hood". These are mostly described in the framework API, but since they are not mentioned in the official guide (http://www.yiiframework.com/doc/guide/) or only mentioned very briefly, only experienced Yii developers usually use these. However, the features described here are relatively simple and using them makes development with Yii much more fun and productive.

 

Using getters and setters


Yii has many features that came from other languages, such as Java or C#. One of them is defining properties with getters and setters for any of the classes extended from CComponent (that is, virtually any Yii class).

From this recipe, you will learn how to define your own properties using getters and setters, how to make your properties read-only, and how to hide custom processing behind native PHP assignments.

How to do it...

  1. As PHP does not have properties at the language level, we can only use getters and setters in the following way:

    class MyClass
    {
        // hiding $property
        private $property;
        
        // getter
        public function getProperty()
        {
            return $this->property;
        }
        
        // setter
        public function setProperty($value)
        {
            $this->property = $value;
        }
    }
    
    $object = new MyClass();
    
    // setting value
    $object->setProperty('value');
    
    // getting value
    echo $object->getProperty();
  2. This syntax is very common in the Java world but it is a bit long to use in PHP. Still, we want to use the same functionality that C# properties gives us: calling getters and setters like class members ($model->property instead of $model->getProperty()). With Yii, we can do it in the following way:

    // extending CComponent is necessary
    class MyClass extends CComponent
    {
        private $property;
    
        public function getProperty()
        {
            return $this->property;
        }
    
        public function setProperty($value)
        {
            $this->property = $value;
        }
    }
    
    $object = new MyClass();
    $object->property = 'value'; // same as $object->setProperty('value');
    echo $object->property; // same as $object->getProperty();
  3. Using this feature, you can make properties read-only or write-only while keeping the simple PHP syntax as follows:

    class MyClass extends CComponent
    {
        private $read = 'read only property';
        private $write = 'write only property';
    
        public function getRead()
        {
            return $this->read;
        }
    
        public function setWrite($value)
        {
            $this->write = $value;
        }
    }
    
    $object = new MyClass();
    
    // gives us an error since we are trying to write
    // to read-only property. Note that there's no setRead setter // method.
    $object->read = 'value'; 
    
    // echoes 'read only property'
    echo $object->read; 
    
    // gives us an error since we are trying to read
    // to write-only property. Note that there's no getWrite getter // method.
    echo $object->write; 
    
    // writes 'value' to private $write
    $object->write = 'value';

Yii uses this technique extensively because almost everything is a component. For example, when you call Yii::app()->user->id to get the currently logged in user ID, what's really called is Yii::app()->getUser()->getId().

How it works...

To use getters and setters like properties, CComponent uses the PHP magic methods: __get, __set, __isset, and __unset (http://php.net/manual/en/language.oop5.magic.php). The following example shows what Yii 1.1 CComponent::__get looks like:

public function __get($name)
{
   $getter='get'.$name;
   if(method_exists($this,$getter))
      return $this->$getter();
…

This magic PHP method intercepts all calls to missing real properties, so when we call $myClass->property, it receives property as the $name parameter. If a method named getProperty exists, then PHP uses its return value as a property value.

There's more...

For further information, refer to the following URL:

http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

See also

  • The Configuring components recipe

             
About the Authors
  • Alexander Makarov

    Alexander Makarov is an experienced engineer from Russia and has been a Yii framework core team member since 2010. Before joining the Yii core team, he participated in the growth of the CodeIgniter community in Russia. In 2009, he finished the Russian translation of the framework documentation and created the Russian community website. In 2012, he released the Russian version of the book along with Russian community members. In the same year, he was a technical reviewer for three more books: The Yii Book: Developing Web Applications Using the Yii PHP Framework, Larry Ullman Web Application Development with Yii and PHP, Jeff Winesett Yii Rapid Application Development Hotshot, Lauren O'Meara, and James Hamilton In his free time, Alexander writes a technical blog at http://rmcreative.ru/, speaks at conferences, and enjoys movies, music, traveling, photography, and languages. He currently resides in Voronezh, Russia with his beloved wife and daughter.

    Browse publications by this author
  • Yii
Yii Application Development Cookbook - Second Edition
Unlock this book and the full library FREE for 7 days
Start now