Skip to content

Commit 9d5da31

Browse files
committed
readme: updated
1 parent ce23456 commit 9d5da31

File tree

1 file changed

+21
-141
lines changed

1 file changed

+21
-141
lines changed

readme.md

Lines changed: 21 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -7,155 +7,35 @@ Nette Utility Classes
77
[![Latest Stable Version](https://poser.pugx.org/nette/utils/v/stable)](https://github.com/nette/utils/releases)
88
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/nette/utils/blob/master/license.md)
99

10-
Install it using Composer:
1110

12-
```
13-
composer require nette/utils
14-
```
15-
16-
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:
55-
56-
![](https://files.nette.org/git/doc-2.1/debugger-circle.png)
57-
58-
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.
11+
Introduction
12+
------------
6113

62-
```php
63-
$circle = new Circle;
64-
echo $circle->undeclared; // throws Nette\MemberAccessException
65-
$circle->undeclared = 1; // throws Nette\MemberAccessException
66-
$circle->unknownMethod(); // throws Nette\MemberAccessException
67-
```
68-
69-
But it has much more to offer!
70-
71-
72-
Properties, getters and setters
73-
-------------------------------
74-
75-
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:
7815

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)
16+
- [Arrays](https://doc.nette.org/arrays) - manipulate arrays
17+
- [Callback](https://doc.nette.org/callbacks) - PHP callbacks
18+
- [Date and Time](https://doc.nette.org/datetime) - modify times and dates
19+
- [Filesystem](https://doc.nette.org/filesystem) - copying, renaming, …
20+
- [HTML elements](https://doc.nette.org/html-elements) - generate HTML
21+
- [Images](https://doc.nette.org/images) - crop, resize, rotate images
22+
- [JSON](https://doc.nette.org/json) - encoding and decoding
23+
- [Generating Random Strings](https://doc.nette.org/random)
24+
- [Pagination](https://doc.nette.org/pagination) - comfort pagination
25+
- [Strings](https://doc.nette.org/strings) - useful text transpilers
26+
- [SmartObject](https://doc.nette.org/smartobject) - PHP Object Enhancements
27+
- [Validation](https://doc.nette.org/validators) - validate inputs
8328

84-
We will make use of properties in the class Circle to make sure variable `$radius` contains only non-negative numbers:
29+
Documentation can be found on the [website](https://doc.nette.org/utils).
8530

86-
```php
87-
/**
88-
* @property float $radius
89-
* @property-read float $area
90-
* @property-read bool $visible
91-
*/
92-
class Circle
93-
{
94-
use Nette\SmartObject;
9531

96-
private $radius; // not public anymore!
32+
Installation
33+
------------
9734

98-
protected function getRadius()
99-
{
100-
return $this->radius;
101-
}
35+
The recommended way to install is via Composer:
10236

103-
protected function setRadius($radius)
104-
{
105-
// sanitizing value before saving it
106-
$this->radius = max(0.0, (float) $radius);
107-
}
108-
109-
protected function getArea()
110-
{
111-
return $this->radius * $this->radius * M_PI;
112-
}
113-
114-
protected function isVisible()
115-
{
116-
return $this->radius > 0;
117-
}
118-
119-
}
120-
121-
$circle = new Circle;
122-
$circle->radius = 10; // calls setRadius()
123-
echo $circle->area; // calls getArea()
124-
echo $circle->visible; // calls $circle->isVisible()
12537
```
126-
127-
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
15939
```
16040

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

Comments
 (0)