This article mainly shares with you what you need to pay attention to with getters and setters in yii2, hoping to help you better master the use of getters and setters in yii2.
Let us first take a look at the description of the authoritative guide in yiichina:
The names of such attributes are not case-sensitive. For example, $object->label and $object->Label are the same attribute. Because PHP method names are not case-sensitive.
If the name of this type of attribute is the same as the class member variable, the latter shall prevail. For example, suppose the above Foo class has a label member variable, Then assign a value to $object->label = 'abc', which will be assigned to the member variable instead of the setter setLabel() method.
This type of property does not support visibility (access restrictions). Whether a property's getter and setter methods are public, protected, or private has no effect on the property's visibility.
The getter and setter methods of this type of property can only be defined as non-static. If they are defined as static methods (static), they will not be processed in the same way.
Normal call to property_exists() that cannot determine the magic property. You should call canGetProperty() or canSetProperty().
If the name of this type of attribute is the same as the class member variable, the latter shall prevail. For example, suppose the above Foo class has a label member variable, and then assigning a value to $object->label = ‘abc’ will be assigned to the member variable instead of the setter setLabel() method.
In fact, this sentence means that our setters and getters only work on hidden properties and protected properties. If it is a variable modified by the public modifier, it will be assigned and obtained directly without going through our setter or getter. Example:
<?php /** * Created by PhpStorm. * Author: weiyongqiang <hayixia606@163.com> * Site: www.weiyongqiang.com * Date: 2017/3/6 * Time: 23:29 */ namespace frontend\components; use yii\base\Component; class UserInfo extends Component { public $userid = 11; public $userName; public function __construct(array $config) { parent::__construct($config); } public function setUserId($userId) { echo 123; $this->userId = $userId; } public function getuserid() { //return $this->userId; return 123456; } } 然后实例化组件后的获取userid属性
$userInfo = new UserInfo([]);echo "<pre/>"; //$userInfo->userId = 10;echo $userInfo->userid;exit;
The output result is: 11
That means the userid is The public modifier attribute does not enter the getuserid defined by me at all, so if your attribute is of public type and you want to uniformly process the attributes, you cannot use getter to achieve it.
This article is the original work of Wei Yongqiang and may not be reproduced without permission: MarsWill » Things to note about getters and setters in yii2.
Related recommendations:
Basic introduction to getters and setters in Javascript
##Talk about the thoughts about getters and setters caused by Vue.js
Object accessor properties in ECMAScript5: Introduction to getters and setters_javascript skills
The above is the detailed content of What should we pay attention to with getters and setters in yii2?. For more information, please follow other related articles on the PHP Chinese website!