A new experimental PHP extension
“scalar_objects” has appeared on Github, with which you can add any methods to numbers, strings, arrays.
It looks like this:
$result = $string->replace('shit', 'candy')->remove(',')->toUpper()->split(" ")->sort();
Nice, isn't it?
Installation instructions can be found at the link above, PHP 5.4 is required.
Note that the extension is in a very early alpha version, the first commit is dated January 24th.
I am glad that the author of the extension is PHP contributor Nikita Popov: there is a possibility that such an API will someday be added to the interpreter core.
Then I just show you how to use these new features in the development.
Suppose we have this line:
$string = 'lemon, orange, shit, banana, apple';
Task:
- replace shit with candy;
- remove commas;
- convert the string to upper case;
- separate the words and put them into an array;
- sort this array alphabetically.
This is usually done like this:
')
$string = str_replace('shit', 'candy', $string); $string = str_replace(',', '', $string); $string = strtoupper($string); $array = explode(' ', $string); sort($array);
With this extension, the problem is solved in one line:
$result = $string->replace('shit', 'candy')->remove(',')->toUpper()->split(" ")->sort();
Everything is clear and beautiful, but how is this to turn?
1. Install the scalar_objects extension;
2. Create handler classes for strings and arrays with the public methods we need:
class StringHandler { public function replace($from, $to) { return str_replace($from, $to, $this); } public function split($separator, $limit = PHP_INT_MAX) { return explode($separator, $this, $limit); } public function toUpper() { return strtoupper($this); } public function remove($what) { return $this->replace($what, ''); } } class ArrayHandler { public function sort($flags = SORT_REGULAR) { sort($this, $flags); return $this; } public function count() { return count($this); } }
3. Assign methods for strings and arrays to the appropriate classes:
register_primitive_type_handler('string', 'StringHandler'); register_primitive_type_handler('array', 'ArrayHandler');
After this, the string variables have the replace, split, remove, and so on methods, the arrays have the count () method. Naturally, you can add as many useful methods as you like. Please note that all of them are public, and $ this is the processed string (array, number). Similarly, handlers can be assigned to any scalar type.
It should also be noted that the structure of the form:
"foobar"->trim();
cause Parse error, that is, methods can be called so far only in variables.
Also in the repository there is an example of the implementation of the convenient “string requests” API:
Very clear and much easier to memorize, unlike strspn, strcspn, strpbrk.
I want to believe that PHP developers will include similar functionality in the next versions. A little bit of such syntactic sugar will make development much nicer, and developers - happier.
Link to the project:
https://github.com/nikic/scalar_objects/