Commit 2d736bf9 by Larry Ullman

Very light edits

parent dc0172b8
...@@ -2,19 +2,19 @@ Properties ...@@ -2,19 +2,19 @@ Properties
========== ==========
In PHP, class member variables are also called *properties*. These variables are part of the class definition, and are used In PHP, class member variables are also called *properties*. These variables are part of the class definition, and are used
to represent the state of a class instance (i.e., to differentiate one instance of the class from another). In practice, you may often want to handle the reading or writing of properties in special ways. For example, you may want to trim a string when it is being assigned to represent the state of a class instance (i.e., to differentiate one instance of the class from another). In practice, you may often want to handle the reading or writing of properties in special ways. For example, you may want to always trim a string when it is being assigned
to a `label` property. You could use the following code to achieve this task: to a `label` property. You *could* use the following code to achieve this task:
```php ```php
$object->label = trim($label); $object->label = trim($label);
``` ```
The drawback of the above code is that you have to call `trim()` everywhere in your code where you might set the `label` The drawback of the above code is that you would have to call `trim()` everywhere in your code where you might set the `label`
property. If in the future, the `label` property gets a new requirement, such as the first letter must be captialized, you would again have to modify every bit of code that assigns a value to `label`. The repetition of code leads to bugs and is a practice you want to avoid as much as possible. property. If, in the future, the `label` property gets a new requirement, such as the first letter must be captialized, you would again have to modify every bit of code that assigns a value to `label`. The repetition of code leads to bugs, and is a practice you want to avoid as much as possible.
To solve this problem, Yii introduces a base class called [[yii\base\Object]] that supports defining properties To solve this problem, Yii introduces a base class called [[yii\base\Object]] that supports defining properties
based on *getter* and *setter* class methods. If a class needs such support, it should extend from based on *getter* and *setter* class methods. If a class needs that functionality, it should extend from
[[yii\base\Object]] or a child class. [[yii\base\Object]], or a child class.
> Info: Nearly every core class in the Yii framework extends from [[yii\base\Object]] or a child class. > Info: Nearly every core class in the Yii framework extends from [[yii\base\Object]] or a child class.
This means that whenever you see a getter or setter in a core class, you can use it like a property. This means that whenever you see a getter or setter in a core class, you can use it like a property.
...@@ -69,9 +69,9 @@ There are several special rules for, and limitations on, the properties defined ...@@ -69,9 +69,9 @@ There are several special rules for, and limitations on, the properties defined
This is because method names in PHP are case-insensitive. This is because method names in PHP are case-insensitive.
* If the name of such a property is the same as a class member variable, the latter will take precedence. * If the name of such a property is the same as a class member variable, the latter will take precedence.
For example, if the above `Foo` class has a member variable `label`, then the assignment `$object->label = 'abc'` For example, if the above `Foo` class has a member variable `label`, then the assignment `$object->label = 'abc'`
will affect the member variable 'label', that line would not call the `setLabel()` setter method. will affect the *member variable* 'label'; that line would not call the `setLabel()` setter method.
* These properties do not support visibility. It makes no difference for the visibility of a property * These properties do not support visibility. It makes no difference for the visibility of a property
if the defining getter or setter method is public, protected or private. if the defining getter or setter method is public, protected or private.
* The properties can only be defined by *non-static* getters and/or setters. Static methods will not be treated in this same manner. * The properties can only be defined by *non-static* getters and/or setters. Static methods will not be treated in this same manner.
Returning back to the problem described at the beginning of this guide, instead of calling `trim()` everywhere a `label` value is assigned, `trim()` only needs to be invoked within the setter `setLabel()`. And if a new requirement comes that requires the label be initially capitalized, the `setLabel()` method can quickly be modified without touching any other code. The one change will universally affect every assignment to `label`. Returning back to the problem described at the beginning of this guide, instead of calling `trim()` everywhere a `label` value is assigned, `trim()` now only needs to be invoked within the setter `setLabel()`. And if a new requirement comes that requires the label be initially capitalized, the `setLabel()` method can quickly be modified without touching any other code. The one change will universally affect every assignment to `label`.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment