# Add \Countable support to \Closure

- Thread: https://externals.io/message/54453
- Original archive: https://news-web.php.net/php.internals/54453
- Started by Ivan Enderlin @ Hoa on 2011-08-08T07:16:19+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #54453 — Ivan Enderlin @ Hoa — 2011-08-08T07:16:19+00:00

URL: https://externals.io/message/54453#54453

Hi Internals,

I would like to suggest you a new feature (for PHP5.4 or maybe next). 
The idea is to implement the \Countable interface into closures to 
easily count the arguments number. I know that it is already possible 
with reflection [1] but it could be a comfortable trick I think.
Subsidiary questions: do we include optional arguments in our calculation?

Here is a trivial example:

    class Closure implements \Countable { … }
    $f = function ( $x, $y, $z ) { … };
    var_dump(count($f));
    // int(3)

Javascript already implements this [2]:

    var f = function ( x, y, z ) { … };
    console.log(f.length);


Thoughts?


Best regards.

[1] <http://php.net/reflectionfunctionabstract.getnumberofparameters>
[2] 
<https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/length>

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #54455 — johannes@schlueters.de — 2011-08-08T07:45:01+00:00

URL: https://externals.io/message/54453#54455 · In reply to: #54453

> Hi Internals,
> 
> I would like to suggest you a new feature (for PHP5.4 or maybe next). 
> The idea is to implement the \Countable interface into closures to 
> easily count the arguments number.

NO!
One could add a getParmNo() or such if really needed while I think
reflection is better (as it also gives parameter information about
required/optional and type "hints")

Countable implies ArrayAccess or such.

johannes

---

## #54457 — Ivan Enderlin @ Hoa — 2011-08-08T08:26:30+00:00

URL: https://externals.io/message/54453#54457 · In reply to: #54455

> 
> > […]
> NO!
> One could add a getParmNo() or such if really needed while I think
> reflection is better (as it also gives parameter information about
> required/optional and type "hints")
>
> Countable implies ArrayAccess or such.
If it is the semantics you ascribe to \Countable, then ok, there is no 
problem :-).

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #54458 — Etienne Kneuss — 2011-08-08T08:49:56+00:00

URL: https://externals.io/message/54453#54458 · In reply to: #54455

Hello,

2011/8/8 Johannes Schlüter <johannes@schlueters.de>:
> 
> > […]
>
> NO!
> One could add a getParmNo() or such if really needed while I think
> reflection is better (as it also gives parameter information about
> required/optional and type "hints")
>
> Countable implies ArrayAccess or such.

I don't believe Countable implies ArrayAccess, but I do think that
Countable should only be implemented in cases where it is obvious what
it will return, for example collections.

In this case, count(Closure) is really not that explicit, I'd rather
have an explicit method.

Best,

>
> johannes
>
>
>
> --
> 
> 
>
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #54460 — Sebastian Bergmann — 2011-08-08T10:55:20+00:00

URL: https://externals.io/message/54453#54460 · In reply to: #54458

> I don't believe Countable implies ArrayAccess, but I do think that
> Countable should only be implemented in cases where it is obvious what
> it will return, for example collections.
>
> In this case, count(Closure) is really not that explicit, I'd rather
> have an explicit method.

  ACK

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #54461 — Alexey Shein — 2011-08-08T11:06:53+00:00

URL: https://externals.io/message/54453#54461 · In reply to: #54460

I think it's just a bad habit inherited from Javascript, where you can
do like this:
var a = function(a, b) { };
alert(a.length); // shows 2

I don't think that PHP needs it, explicit method is way better.

2011/8/8 Sebastian Bergmann <sebastian@php.net>:
> 
> > […]
>
>  ACK
>
> --
> Sebastian Bergmann                    Co-Founder and Principal Consultant
> http://sebastian-bergmann.de/                           http://thePHP.cc/
>
> --
> 
> 
>
>



-- 
Regards,
Shein Alexey

---

## #54462 — Paul Dragoonis — 2011-08-08T11:16:18+00:00

URL: https://externals.io/message/54453#54462 · In reply to: #54461

> I think it's just a bad habit inherited from Javascript, where you can
> do like this:
> var a = function(a, b) { };
> alert(a.length); // shows 2
>
> I don't think that PHP needs it, explicit method is way better.


I am agreeing with the fact that we don't need any more magic in PHP
and explicit methods go much further than any magic you can add to a
language.

>
> 2011/8/8 Sebastian Bergmann <sebastian@php.net>:
> > […]
>
>
>
> --
> Regards,
> Shein Alexey

---

## #54464 — Ivan Enderlin @ Hoa — 2011-08-08T11:43:02+00:00

URL: https://externals.io/message/54453#54464 · In reply to: #54462

> 
> > […]
>
> I am agreeing with the fact that we don't need any more magic in PHP
> and explicit methods go much further than any magic you can add to a
> language.
I'm also agreeing :-). It was just a proposition.

Cheers.

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

Member of HTML and WebApps Working Group of W3C
http://w3.org/
