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
Copy file name to clipboardExpand all lines: utils/en/smartobject.texy
+23-23Lines changed: 23 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@ SmartObject
2
2
***********
3
3
4
4
.[perex]
5
-
SmartObject used to fix objects behavior in many ways, but today's PHP already includes most of these improvements natively. However, it still adds support for *property*.
5
+
SmartObject used to fix objects behavior in many ways, but today's PHP already includes most of these improvements natively. However, it still adds support for *properties*.
Properties are primarily "syntactic sugar"((syntactic sugar)), which is intended to make the programmer's life sweeter by simplifying the code. If you don't want them, you don't have to use them.
66
66
67
67
68
-
A Look into the History
69
-
=======================
68
+
A Glimpse into History
69
+
======================
70
70
71
-
SmartObject used to fix object behavior in many ways, but today's PHP already includes most of the improvements natively. The following is a nostalgic look back in history and a reminder of how things have evolved.
71
+
SmartObject used to refine the behavior of objects in numerous ways, but today's PHP already incorporates most of these enhancements natively. The following text is a nostalgic look back at history, reminding us of how things evolved.
72
72
73
-
From the beginning, the PHP object model suffered from a number of serious flaws and inefficiencies. This was the reason for the creation of the `Nette\Object` class (in 2007), which attempted to remedy them and improve the experience of using PHP. It was enough for other classes to inherit from it, and gain the benefits it brought. When PHP 5.4 came with trait support, the `Nette\Object` class was replaced by `Nette\SmartObject`. Thus, it was no longer necessary to inherit from a common ancestor. In addition, trait could be used in classes that already inherited from another class. The final end of `Nette\Object` came with the release of PHP 7.2, which forbade classes to be named `Object`.
73
+
From its inception, PHP's object model suffered from a myriad of serious shortcomings and deficiencies. This led to the creation of the `Nette\Object` class (in 2007), which aimed to rectify these issues and enhance the comfort of using PHP. All that was needed was for other classes to inherit from it, and they would gain the benefits it offered. When PHP 5.4 introduced support for traits, the `Nette\Object` class was replaced by the `Nette\SmartObject` trait. This eliminated the need to inherit from a common ancestor. Moreover, the trait could be used in classes that already inherited from another class. The definitive end of `Nette\Object` came with the release of PHP 7.2, which prohibited classes from being named `Object`.
74
74
75
-
As PHP development went on, the object model and language capabilities were improved. The individual functions of the `SmartObject` class became redundant. Since the release of PHP 8.2, the only feature that remains that is not yet directly supported in PHP is the ability to use so-called [properties|#Properties, Getters and Setters].
75
+
As PHP development continued, its object model and language capabilities improved. Various functions of the `SmartObject` class became redundant. Since the release of PHP 8.2, there remains only one feature not directly supported in PHP: the ability to use so-called [properties|#Properties, getters, and setters].
76
76
77
-
What features did `Nette\Object` and`Nette\Object` once offer? Here is an overview. (The examples use the `Nette\Object` class, but most of the properties also apply to the `Nette\SmartObject` trait.)
77
+
What features did `Nette\Object` and, by extension, `Nette\SmartObject` offer? Here's an overview. (In the examples, the `Nette\Object` class is used, but most features also apply to the `Nette\SmartObject` trait).
$obj->unknownMethod(); // Fatal error (not catchable by try/catch)
88
88
```
89
89
90
-
Fatal error terminated the application without any possibility to react. Silently writing to non-existent members without warning could lead to serious errors that were difficult to detect. `Nette\Object` All of these cases were caught and an exception `MemberAccessException` was thrown.
90
+
A fatal error would terminate the application without any chance of response. Silently writing to non-existent members without warning could lead to serious errors that were hard to detect. `Nette\Object` caught all these cases and threw a `MemberAccessException` exception.
Since PHP 7.0, PHP no longer causes not catchable fatal errors, and accessing undeclared members has been a bug since PHP 8.2.
97
+
From PHP version 7.0 onwards, uncatchable fatal errors no longer occur, and accessing undeclared members becomes an error from PHP 8.2.
98
98
99
99
100
100
Did you mean?
@@ -110,16 +110,16 @@ class Foo extends Nette\Object
110
110
}
111
111
112
112
$foo = Foo::form($var);
113
-
// throw Nette\MemberAccessException
113
+
// throws Nette\MemberAccessException
114
114
// "Call to undefined static method Foo::form(), did you mean from()?"
115
115
```
116
116
117
-
Today's PHP may not have any form of "did you mean?", but [Tracy |tracy:] adds this addendum to errors. And it can even [fix |tracy:open-files-in-ide#toc-demos] such errors itself.
117
+
While today's PHP doesn't have a "did you mean?" feature, this phrase can be added to errors by [Tracy|tracy:]. It can even [auto-correct such errors|tracy:open-files-in-ide#toc-demos].
118
118
119
119
120
-
Extension methods
120
+
Extension Methods
121
121
-----------------
122
-
Inspired by extension methods from C#. They gave the possibility to add new methods to existing classes. For example, you could add the `addDateTime()` method to a form to add your own DateTimePicker.
122
+
Inspired by the extension methods from the C# language, they provided the ability to add new methods to existing classes. For instance, you could add a `addDateTime()` method to a form, which would introduce a custom DateTimePicker.
123
123
124
124
```php
125
125
Form::extensionMethod(
@@ -131,15 +131,15 @@ $form = new Form;
131
131
$form->addDateTime('date');
132
132
```
133
133
134
-
Extension methods proved to be impractical because their names was not autocompleted by editors, instead they reported that the method did not exist. Therefore, their support was discontinued.
134
+
Extension methods turned out to be impractical because their names were not suggested by editors; on the contrary, they reported that the method did not exist. Therefore, their support was discontinued.
135
135
136
136
137
-
Getting the Class Name
138
-
----------------------
137
+
Determining the Class Name
138
+
--------------------------
139
139
140
140
```php
141
141
$class = $obj->getClass(); // using Nette\Object
142
-
$class = $obj::class; // since PHP 8.0
142
+
$class = $obj::class; // from PHP 8.0
143
143
```
144
144
145
145
@@ -158,7 +158,7 @@ class Foo extends Nette\Object
158
158
159
159
$obj = new Foo;
160
160
$reflection = $obj->getReflection();
161
-
$reflection->getAnnotation('author'); // returns 'John Doe
As of PHP 8.0, it is possible to access meta-information in the form of attributes:
@@ -216,7 +216,7 @@ class Circle extends Nette\Object
216
216
public function setRadius(float $radius): void
217
217
{
218
218
$this->onChange($this, $radius);
219
-
$this->radius = $radius
219
+
$this->radius = $radius;
220
220
}
221
221
}
222
222
```
@@ -229,7 +229,7 @@ foreach ($this->onChange as $callback) {
229
229
}
230
230
```
231
231
232
-
For the sake of clarity we recommend to avoid the magic method `$this->onChange()`. A practical substitute is the [Nette\Utils\Arrays::invoke |arrays#invoke] function:
232
+
For clarity, we recommend avoiding the magic method `$this->onChange()`. A practical alternative is the [Nette\Utils\Arrays::invoke |arrays#invoke] function:
0 commit comments