- Yii 1.1 Application Development Cookbook
- Alexander Makarov
- 478字
- 2021-04-02 18:40:57
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...
- 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();
- 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();
- 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';
- 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 isYii::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
- SOLIDWORKS 2021中文版基礎入門一本通
- 快學熟用D3
- SPSS統計分析
- Vue.js快速入門
- 邊做邊學:Photoshop CS6數碼藝術照片后期處理教程
- 中文版3ds Max 2016/VRay效果圖制作實戰基礎教程(全彩版)
- Origin 2022科學繪圖與數據分析(高級應用篇)
- 機械CAD軟件應用入門指導書
- 攝影師的后期必修課(RAW格式篇)
- 零基礎學后期:Lightroom 6-CC數碼照片處理從新手到高手
- SolidWorks三維設計及工程圖速成
- 從零開始:Photoshop CC 2019設計基礎+商業設計實戰
- 中文版AutoCAD 2021完全自學教程
- Photoshop CC 2015課堂實錄
- MATLAB 2008全程指南