Book Image

Yii 1.1 Application Development Cookbook

Book Image

Yii 1.1 Application Development Cookbook

Overview of this book

When Alex told me he was about to write a Yii cookbook about a year ago, I was wondering how original it would be, considering the fact that there was already an online user-contributed cookbook (aka. Yii wiki). It turned out Alex produced a book that is not only full of wisdom about how to use Yii effectively, but also presented in such a systematic way that it can be taken as an essential companion book to the definitive guide to Yii. In fact, Alex has successfully intrigued the interest of every member in the Yii developer team when he asked for review and comments on his newly finished book chapters.As the founder and the lead developer of the Yii framework, I feel this book is a must-read for every Yii programmer. While this book does not describe directly the rules set by Yii, it shows how to program with Yii from a practical perspective. People who are driven by tight project schedules will find this book very handy as it gives ready-to-use solutions to many problems they may face in their projects; people who are already familiar with Yii will also find this book very informative as most problem solutions given in the book can be considered as officially recommended because they have undergone thorough review of every Yii developer team member. Alex, through this book and his active participation in the Yii project, proved himself to be a great programmer as well as a good writer. Qiang XueLead developer of the Yii framework Yii framework is a rapidly growing PHP5 MVC framework often referred to as Rails for PHP. It has become a solid base for many exciting web applications such as Stay.com and Russia Today's meetfriends.rt.com and can be a good base for your developments. Yii is an object-oriented, high-performance, component-based PHP web application framework. Yii is pronounced as Yee and is an acronym for "Yes It Is!". Familiar with Yii and want to exploit it to its full potential, but do not know how to go about it? Yii 1.1 Application Development Cookbook will show you how to use Yii efficiently. You will learn about implementing shortcuts using core features, creating your own reusable code base, using test-driven development, and many more topics that will escalate your knowledge in no time at all! Yii 1.1 Application Development Cookbook will help you learn more about Yii framework and application development practices in general with demonstrations of shortcuts and information about dangerous things you should not do. Grouped in 13 chapters, the recipes will assist you to write your applications exploiting Yii core functionality to its full potential. The chapters are generally independent of each other and you can start reading from the chapter you need most, whether it is "AJAX and jQuery", "Database, Active Record and Model Tricks" or "Extending Yii". The most interesting topics include Yii application deployment, a guide to writing your own extensions, advanced error handling, debugging and logging, application security, and performance tuning. Yii 1.1 Application Development Cookbook will help you utilize Yii functionalities completely and efficiently.
Table of Contents (21 chapters)
Yii 1.1 Application Development Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 class 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 C# properties gives us: calling getters and setters like class members. 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
    $object->read = 'value'; 
    
    // echoes 'read only property'
    echo $object->read; 
    
    // gives us an error since we are trying to read to write-only property
    echo $object->write; 
    
    // writes 'value' to private $write
    $object->write = 'value';
  4. Yii uses this technique extensively because almost everything is a component. For example, when you are calling 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 are calling $myClass->property, it receives property as $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 recipe named Using Yii events in this chapter

  • The recipe named Configuring components in this chapter