php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62783 Request of using methods for core types.
Submitted: 2012-08-09 00:38 UTC Modified: 2017-09-21 08:40 UTC
Votes:21
Avg. Score:4.2 ± 1.2
Reproduced:13 of 19 (68.4%)
Same Version:11 (84.6%)
Same OS:8 (61.5%)
From: hinikato at mail dot ru Assigned:
Status: Suspended Package: Class/Object related
PHP Version: 5.4.6RC1 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: hinikato at mail dot ru
New email:
PHP Version: OS:

 

 [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.




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-09 00:50 UTC] hinikato at mail dot ru
-Operating System: Windows 7 +Operating System:
 [2012-08-09 00:50 UTC] hinikato at mail dot ru
Removed the non-relevant field for the issue description.
 [2012-08-09 03:35 UTC] 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 04:10 UTC] hinikato at mail dot ru
@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 ;)
 [2012-08-09 06:08 UTC] hinikato at mail dot ru
One more important addition: I suggest to introduce camelCased() methods because we have PSR-2 standard and SPL.
 [2012-09-28 15:20 UTC] hinikato at mail dot ru
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..
 [2012-09-28 15:24 UTC] hinikato at mail dot ru
..and the new Strings() should be written as: new String("foo") or just "foo"->bar()
 [2017-09-21 08:40 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2017-09-21 08:40 UTC] cmb@php.net
Thank you for your interest in PHP and for submitting a feature request. Please
be aware that due to the magnitude of change this request requires, it would be
necessary to discuss it on PHP Internals list (internals@lists.php.net) as an
RFC. Please read the guide about creating RFCs here:
<https://wiki.php.net/rfc/howto>. If you haven't had experience with writing
RFCs before, it is advised to seek guidance on the Internals list
(<http://php.net/mailing-lists.php>) and/or solicit help from one of the
experienced developers. 

Please do not consider this comment as a negative view on the merits of your
proposal – every proposal which requires changes of certain magnitude, even the
very successful and widely supported ones, must be done through the RFC process.
This helps make the process predictable, transparent and accessible to all
developers.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC