Req #62783 [Opn]: Request of using methods for core types.
| From: | hinikato at mail dot ru | Date: | Thu, 09 Aug 2012 04:10:19 +0000 |
| Subject: | Req #62783 [Opn]: Request of using methods for core types. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-172516@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62783&edit=1
ID: 62783
User updated by: hinikato at mail dot ru
Reported by: hinikato at mail dot ru
Summary: Request of using methods for core types.
Status: Open
Type: Feature/Change Request
Package: Class/Object related
PHP Version: 5.4.6RC1
Block user comment: N
Private report: N
New Comment:
@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 ;)
Previous Comments:
------------------------------------------------------------------------
[2012-08-09 03:35:23] laruence@php.net
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();
------------------------------------------------------------------------
[2012-08-09 00:50:39] hinikato at mail dot ru
Removed the non-relevant field for the issue description.
------------------------------------------------------------------------
[2012-08-09 00:38:00] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62783&edit=1