|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-08-09 00:38 UTC] hinikato at mail dot ru
Description: ------------ I propose to introduce new methods for the core PHP types like arrays, strings, floats etc? For example it would be super cool to write something like this: [1, 2, 3]->sort() "Hello, World"->lowercase()->ucfirst() etc. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 11:00:01 2025 UTC |
you can already do it with a wrapper of string now: <?php class Strings { private $str = NULL; function __construct($str) { $this->str = $str; } function __call($method, $params) { return new self($method($this->str)); } function __toString() { return $this->str; } } echo (new Strings("Hello, World"))->strtolower()->ucfirst();@laruence@php.net, thank you for this info, I really don't know about such use case of self with combination of the __call() method. However I talk about native implementation that has difference with described wrapper: 1. Performance - native implementation should be more fast because it will be written on C (not PHP). 2. The native implementation is more short and it is more ease to use. The developer don't need to do something like that: <?php foreach ($arrayOfString as &$string) { $string = new Strings($string); } unset($string); ?> to add string's methods for all items of array. It is obviously that such conversions is inconvenient and make our code not so beautiful. 3. Native methods will support exceptions. This mean that old checking like: if ($result == false) { throw new Exception(); } will not be required. This will let us write less code and do more. 4. We will have set of standard methods - as result PHP developers will not develop new wrappers (bicycles) to resolve basic tasks. 5. Native methods will be supported for all strings, arrays etc, it will lead that user will be able to pass any of existing (native) method as callback. 6. It will allow to fix old inconsistent with parameters, so we will not have something like this: array array_diff_ukey ( array $array1 , array $array2 [, array $ ... ], callable $key_compare_func ) array array_map ( callable $callback , array $arr1 [, array $... ] ) bool array_walk_recursive ( array &$input , callable $funcname [, mixed $userdata = NULL ] ) Why the callback is on 1, 2 and third place here??? It is so inconvenient. It is hard to remember and use. I and all my PHP friends need to use PHP docs almost every time when we use any of such functions! Please fix it too ;) 7. It is more object oriented way. PHP will be more beautiful and more logically structured. 8. (optional) User will be able to extend native classes to add missing methods (like Ruby, JavaScript allows to do). -- All developers that I know including me would like to have such features in PHP ;)One adddition: the following example above: echo (new Strings("Hello, World"))->strtolower()->ucfirst(); should be rewritten as follows: echo (new Strings("Hello, World"))->tolowercase()->ucfirst(); or as follows: echo (new Strings("Hello, World"))->downcase()->ucfirst(); to be more clear....and the new Strings() should be written as: new String("foo") or just "foo"->bar()