# Regarding constructions like func()[0]

- Thread: https://externals.io/message/45970
- Original archive: https://news-web.php.net/php.internals/45970
- Started by melfar on 2009-11-07T11:42:16+00:00
- Messages: 13
- 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

---

## #45970 — melfar — 2009-11-07T11:42:16+00:00

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

Hello!

I have filed a bug (suggestion) at http://bugs.php.net/bug.php?id=50003
What do you think about enabling such a constructions in future versions of 
php?

Br,
-melfar

---

## #45971 — mm w — 2009-11-07T16:48:57+00:00

URL: https://externals.io/message/45970#45971 · In reply to: #45970

Hi Melfar, from my point of view I am totally to make this a legal
call like in python or ruby java,
as I liked compact code I liked to avoid temp vars especially on a
read-only call, but it might require some effort there and the final
patch might be not so simple, anyway this is a community call, what I
can say about this kind of missing feature "sometimes, I can hear in
my office" : "PHP is a retarded language, PHP sucks or worst", my
point there, that is sometimes good to listen to people who are not
"passionate" by the language but just using it to do their job and be
paid.

Cheers!


> Hello!
>
> I have filed a bug (suggestion) at http://bugs.php.net/bug.php?id=50003
> What do you think about enabling such a constructions in future versions of
> php?
>
> Br,
> -melfar

---

## #45972 — melfar — 2009-11-07T23:29:23+00:00

URL: https://externals.io/message/45970#45972 · In reply to: #45971

Hey,

Yep, I think there might be some caveats to that change.
The original patch did not work on methods, so I've fixed that and also
made the return value an rvalue, so that you can't attempt to modify the
temporary value, the zend_check_writable_variable guard will yield an error.

The updated patch can be found at http://bugs.php.net/bug.php?id=50003

I've also heard an opinion that adding this construction is just going to 
make things more
complicated, but take this: chained -> operators are supported for return
values and I'm only seeing code getting simpler because of that.

Br,
-melfar

"mm w" <0xcafefeed@gmail.com> wrote:

> Hi Melfar, from my point of view I am totally to make this a legal
> call like in python or ruby java,
> as I liked compact code I liked to avoid temp vars especially on a
> read-only call, but it might require some effort there and the final
> patch might be not so simple, anyway this is a community call, what I
> can say about this kind of missing feature "sometimes, I can hear in
> my office" : "PHP is a retarded language, PHP sucks or worst", my
> point there, that is sometimes good to listen to people who are not
> "passionate" by the language but just using it to do their job and be
> paid.
>
> Cheers!
>
> 
> > […]

---

## #45973 — Richard Quadling — 2009-11-08T07:44:24+00:00

URL: https://externals.io/message/45970#45973 · In reply to: #45972

2009/11/7 melfar <melfar@gmail.com>:
> Hey,
>
> Yep, I think there might be some caveats to that change.
> The original patch did not work on methods, so I've fixed that and also
> made the return value an rvalue, so that you can't attempt to modify the
> temporary value, the zend_check_writable_variable guard will yield an error.
>
> The updated patch can be found at http://bugs.php.net/bug.php?id=50003
>
> I've also heard an opinion that adding this construction is just going to
> make things more
> complicated, but take this: chained -> operators are supported for return
> values and I'm only seeing code getting simpler because of that.
>
> Br,
> -melfar
>
> "mm w" <0xcafefeed@gmail.com> wrote:
>
> > […]
>
>
>
> --
> 
> 
>
>

Whilst you are all thinking about this, how about ...

$value = (function(){return $x;})();

Being able to call an anonymous function inline would certainly be a
useful mechanism. Allowing calls without creating dummy user land vars
would be consistent with func()[0].

-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

---

## #45974 — jvlad — 2009-11-08T09:23:24+00:00

URL: https://externals.io/message/45970#45974 · In reply to: #45972

""melfar"" <melfar@gmail.com> wrote in message 
news:0F.F6.03668.AD206FA4@pb1.pair.com...
> Hey,
>
> Yep, I think there might be some caveats to that change.
> The original patch did not work on methods, so I've fixed that and also
> made the return value an rvalue, so that you can't attempt to modify the
> temporary value, the zend_check_writable_variable guard will yield an 
> error.
>
> The updated patch can be found at http://bugs.php.net/bug.php?id=50003
>
> I've also heard an opinion that adding this construction is just going to 
> make things more
> complicated, but take this: chained -> operators are supported for return
> values and I'm only seeing code getting simpler because of that.
>
> Br,
> -melfar

- the patch introduces 5 more grammar conflicts. Did you check them all?
- can't call the results useful. Instead of returning possibly long array 
and fetching just
one element, why not to add an argument to the funtion - the index of the 
element
to be returned?
- if you badly need the "function" returning an array and you prefer syntax 
to performance,
think about indexed class property implemented via setters/getters:

class A {
  public function __get($prop) {
    if ($prop == "b")
      return array('a' => 'b');
  }
}

$a = new A();
echo $a->b['a'];

Class instance is where you'll keep the context.
If you don't need any context, static properties would be useful, but they 
can't be
implemented via setters/getters and this is a thing should be changed in 
order to get
the following sample working:

<?php

class A {
  public static function __get($prop) {
    if ($prop == "b")
      return array('a' => 'b');
  }
}

echo A::$b['a'];
?>


I think php is not that bad  after all :)

just 2c.

---

## #45975 — jvlad — 2009-11-08T09:52:04+00:00

URL: https://externals.io/message/45970#45975 · In reply to: #45973

> > […]
>
> Whilst you are all thinking about this, how about ...
>
> $value = (function(){return $x;})();
>
> Being able to call an anonymous function inline would certainly be a
> useful mechanism. Allowing calls without creating dummy user land vars
> would be consistent with func()[0].
>


If you care not to waste userland with new names, use namespaces,
that's the way intruduced by php, and it is not javascript after all.

---

## #45976 — Richard Quadling — 2009-11-08T10:04:57+00:00

URL: https://externals.io/message/45970#45976 · In reply to: #45975

2009/11/8 jvlad <dmda@yandex.ru>:
> > […]
>
>
> If you care not to waste userland with new names, use namespaces,
> that's the way intruduced by php, and it is not javascript after all.
>
>
>
> --
> 
> 
>
>

I'm so old school in my coding, I've not got my head around namespaces
yet. I'm just old.

-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

---

## #45977 — jvlad — 2009-11-09T08:30:00+00:00

URL: https://externals.io/message/45970#45977 · In reply to: #45976

> > […]
>
> I'm so old school in my coding, I've not got my head around namespaces
> yet. I'm just old.
>
There is nothing really "new" in namespaces.
For example the concept of namespaces in c++ was introduced in the beginning 
of 90s.
JavaScript with anonymous functions appeared later, in 95-96.
I'm not about whichever came first, just wanted to mention that neither as a 
feature is new.

---

## #45979 — david.zuelke@bitextender.com — 2009-11-09T09:41:13+00:00

URL: https://externals.io/message/45970#45979 · In reply to: #45970

It's already on the list for PHP6:

http://wiki.php.net/summits/pdmnotesmay09 (see Day 2, PHP 6, #13).

No need to discuss this further, I think. You might also want to have  
that bug closed, it's redundant.

- David





> Hello!
>
> I have filed a bug (suggestion) at http://bugs.php.net/bug.php? 
> id=50003
> What do you think about enabling such a constructions in future  
> versions of
> php?
>
> Br,
> -melfar

---

## #45981 — Philip Olson — 2009-11-09T16:47:43+00:00

URL: https://externals.io/message/45970#45981 · In reply to: #45979

> 
> > […]



> It's already on the list for PHP6:
>
> http://wiki.php.net/summits/pdmnotesmay09 (see Day 2, PHP 6, #13).
>
> No need to discuss this further, I think. You might also want to  
> have that bug closed, it's redundant.

There's also a declined-ish RFC on this matter:

- http://wiki.php.net/rfc/functionarraydereferencing

Regards,
Philip

---

## #45983 — Lukas Kahwe Smith — 2009-11-09T17:56:51+00:00

URL: https://externals.io/message/45970#45983

> > […]
>
>
> > […]
>
> then I don't get the point.
> http://wiki.php.net/summits/pdmnotesmay09  last modified on 07/22/2009
> while the "declinish" last modified on 02/05/2009
> so the proposal is newer and doesn't say anything about status of the
> feature.
>
> It would be much easier to have ONE and only ONE issue for each
> feature planned or proposed for the future versions of php. Just a
> dedicated section at bugs.php.net site would work the best.
> At least much better than "declinish" stuff.


you did read the disclaimer at the top of the pdm notes?
anyways, an in person meeting of several php.net people does not  
constitute a decision. decision are made on this list. ideally they  
are based on an RFC that is then updated according to the discussions  
and decisions on this list. and even then decisions at times will be  
reversed too. so it goes.

regards,
Lukas Kahwe Smith
mls@pooteeweet.org

PS: I do however agree that in the future notes from meetings like  
this should be given a bit more details, like the date, the people who  
attended and stuff like that.

---

## #45986 — jvlad — 2009-11-10T08:41:38+00:00

URL: https://externals.io/message/45970#45986

"Mathieu Suen" <mathieu.suen@easyflirt.com> wrote in message 
news:4AF91F92.4000101@easyflirt.com...
>
> Why isn't a mater of pushing the function call statement into a valuable
> statement?
>
> I guess something like this:
>
> statement :
>  | value
> | function_call;
>
>
> value :
> | valriable
> | array_access
> | method_send
> | ...;
...
> You also could push the new statement into the value so you can do:
>
> (new A())->foo();
>
> or
>
> foo(new A());
>
> -mthk

How many conflicts does it bring?
For example patch offered in this issue http://bugs.php.net/bug.php?id=50003
 brought 5 new conflicts.
Please note that in some cases conflicts will delay calls to zend_compile.c
functions and therefore may bring real problems. IMHO it's not as
simple as populating grammar elements. The results must be carefully tested 
to
avoid BC breakage.


just 2c

---

## #45998 — Stanislav Malyshev — 2009-11-10T17:58:58+00:00

URL: https://externals.io/message/45970#45998 · In reply to: #45973

Hi!

> Whilst you are all thinking about this, how about ...
> 
> $value = (function(){return $x;})();

I tried implementing that but it's not straightforward because you need 
to keep the result of first call while everything inside () is 
calculated (which can be any number of expressions including function 
calls etc.) which wouldn't work with naive implementation, since by 
default it doesn't have correct refcount to be preserved and if you 
change it you need to add cleanup somewhere.
BTW I'm not sure the ()[] works there too - did you try to put some 
complex expressions with multiple fcalls inside []?

-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com
