You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The last stable release requires PHP version 5.6 or newer (is compatible with PHP 7.0 and 7.1). The dev-master version requires PHP 7.0.
17
-
18
-
19
-
Nette\SmartObject: Strict classes
20
-
---------------------------------
21
-
22
-
PHP gives a huge freedom to developers, which makes it a perfect language for making mistakes. But you can stop this bad behavior and start writing applications without hardly discoverable mistakes. Do you wonder how? It's really simple -- you just need to have stricter rules.
23
-
24
-
Can you find an error in this example?
25
-
26
-
```php
27
-
class Circle
28
-
{
29
-
public $radius;
30
-
31
-
public function getArea()
32
-
{
33
-
return $this->radius * $this->radius * M_PI;
34
-
}
35
-
36
-
}
37
-
38
-
$circle = new Circle;
39
-
$circle->raduis = 10;
40
-
echo $circle->getArea(); // 10² * π ≈ 314
41
-
```
42
-
43
-
On the first look it seems that code will print out 314; but it returns 0. How is this even possible? Accidentaly, `$circle->radius` was mistyped to `raduis`. Just a small typo, which will give you a hard time correcting it, because PHP does not say a thing when something is wrong. Not even a Warning or Notice error message. Because PHP does not think it is an error.
44
-
45
-
The mentioned mistake could be corrected immediately, if class `Circle` would use trait Nette\SmartObject:
46
-
47
-
```php
48
-
class Circle
49
-
{
50
-
use Nette\SmartObject;
51
-
...
52
-
```
53
-
54
-
Whereas the former code executed successfully (although it contained an error), the latter did not:
Trait `Nette\SmartObject` made `Circle` more strict and threw an exception when you tried to access an undeclared property. And Tracy displayed error message about it. Line of code with fatal typo is now highlighted and error message has meaningful description: *Cannot write to an undeclared property Circle::$raduis, did you mean $radius?*. Programmer can now fix the mistake he might have otherwise missed and which could be a real pain to find later.
59
-
60
-
One of many remarkable abilities of `Nette\SmartObject` is throwing exceptions when accessing undeclared members.
In modern object oriented languages *property* describes members of class, which look like variables but are represented by methods. When reading or assigning values to those "variables", methods are called instead (so-called getters and setters). It is really useful feature, which allows us to control the access to these variables. Using this we can validate inputs or postpone the computation of values of these variables to the time when it is actually accessed.
76
-
77
-
Any class that uses `Nette\SmartObject` acquires the ability to imitate properties. Only thing you need to do is to keep simple convention:
14
+
In package nette/utils you will find a set of useful classes for everyday use:
78
15
79
-
- Add annotation `@property <type> $name`
80
-
- Getter's name is `getXyz()` or `isXyz()`, setter's is `setXyz()`
81
-
- It is possible to have `@property-read` only and `@property-write` only properties
82
-
- Names of properties are case-sensitive (first letter being an exception)
Properties are mostly a syntactic sugar to beautify the code and make programmer's life easier. You do not have to use them, if you don't want to.
128
-
129
-
Events
130
-
------
131
-
132
-
Now we are going to create functions, which will be called when border radius changes. Let's call it `change` event and those functions event handlers:
133
-
134
-
```php
135
-
class Circle
136
-
{
137
-
use Nette\SmartObject;
138
-
139
-
/** @var array */
140
-
public $onChange;
141
-
142
-
public function setRadius($radius)
143
-
{
144
-
// call all handlers in array $onChange
145
-
$this->onChange($this, $this->radius, $radius);
146
-
147
-
$this->radius = max(0.0, (float) $radius);
148
-
}
149
-
}
150
-
151
-
$circle = new Circle;
152
-
153
-
// adding an event handler
154
-
$circle->onChange[] = function (Circle $circle, $oldValue, $newValue) {
155
-
echo 'there was a change!';
156
-
};
157
-
158
-
$circle->setRadius(10);
38
+
composer require nette/utils
159
39
```
160
40
161
-
There is another syntactic sugar in `setRadius`'s code. Instead of iteration on `$onChange` array and calling each method one by one with unreliable (does not report if callback has any errors) function call_user_func, you just have to write simple `onChange(...)` and given parameters will be handed over to the handlers.
41
+
It requires PHP version 5.6 and supports PHP up to 7.2. The dev-master version requires PHP 7.0.
0 commit comments