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
Trait `Nette\SmartObject` made `Circle` more strict and threw an exception when you tried to access an undeclared property. And `Tracy\Debugger` 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*. Programmer can now fix the mistake he might have otherwise missed and which could be a real pain to find later.
49
+
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.
50
50
51
51
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.
67
67
68
68
Any class that uses `Nette\SmartObject` acquires the ability to imitate properties. Only thing you need to do is to keep simple convention:
69
69
70
-
- Add annotation `@property type $xyz`
70
+
- Add annotation `@property <type> $name`
71
71
- Getter's name is `getXyz()` or `isXyz()`, setter's is `setXyz()`
72
72
- It is possible to have `@property-read` only and `@property-write` only properties
73
73
- Names of properties are case-sensitive (first letter being an exception)
@@ -142,7 +142,7 @@ class Circle
142
142
$circle = new Circle;
143
143
144
144
// adding an event handler
145
-
$circle->onChange[] = function ($circle, $oldValue, $newValue) {
145
+
$circle->onChange[] = function (Circle $circle, $oldValue, $newValue) {
0 commit comments