# New dereferencing syntaxes in 5.4

- Thread: https://externals.io/message/56441
- Original archive: https://news-web.php.net/php.internals/56441
- Started by Nikita Popov on 2011-11-20T15:42:51+00:00
- Messages: 9
- 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

---

## #56441 — Nikita Popov — 2011-11-20T15:42:51+00:00

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

Hi internals!

I want to discuss some details of the new dereferencing syntaxes which
were introduced in 5.4.

1. I already mentioned lack of support for dereferencing using the
alternative array syntax earlier
(http://markmail.org/thread/j6rs3bzgzhxmco7n) but there was no
conclusion on that topic. Now the (new A)[0] dereferencing syntax
landed without alternative syntax too. Does this mean that the general
consensus is to deprecate and remove the {} syntax altogether in the
future?

2. PHP always supported doing $arr[0]() and 5.4 added the reverse
func()[0]. The addition seems slightly inconsistent though, as the
combination of both does not work: func()[0]() will result in a syntax
error. Same applies to method array dereferencing. Is this
intentional?

3. The new expression method, property and array dereferencing
syntaxes were added in expr_without_variable. I am not sure whether
this was intentional or not. That way (new A)->b will not be a
variable (in the parser sense), whereas $a->b will. What is the impact
of this? One thing that will not work is doing (new A)->b = 'c'. I
know, this looks fairly stupid, but if you consider that A might
overload the __set operation it might actually make sense. (It won't
make much sense, but hey, (new A)[0] doesn't make much sense to me
either). Other areas that might be affected are return by reference,
pass by reference and iterate by reference (but I didn't test this).

So, I know, those are all edge cases, so leaving them as is won't hurt
much. Still I think it's better to discuss them ;)

Nikita

---

## #56450 — irker@irker.net — 2011-11-21T07:59:12+00:00

URL: https://externals.io/message/56441#56450 · In reply to: #56441

Amateurish question to internals:
Why can not we do more general cases? For example (expr)[3];
(expr)->foo->bar;
(expr)()->baz[3]()->makeSomething()[4] = 16; etc.

Now we have (new some)->something; and doSomething()[12], but why only this
cases?

Big trouble in parser to do this? Can someone help me to find discussions
about it, if they exist.

With regards, Alexander Moskaliov
Irker@irker.net


2011/11/20 Nikita Popov <nikita.ppv@googlemail.com>

> Hi internals!
>
> I want to discuss some details of the new dereferencing syntaxes which
> were introduced in 5.4.
>
> 1. I already mentioned lack of support for dereferencing using the
> alternative array syntax earlier
> (http://markmail.org/thread/j6rs3bzgzhxmco7n) but there was no
> conclusion on that topic. Now the (new A)[0] dereferencing syntax
> landed without alternative syntax too. Does this mean that the general
> consensus is to deprecate and remove the {} syntax altogether in the
> future?
>
> 2. PHP always supported doing $arr[0]() and 5.4 added the reverse
> func()[0]. The addition seems slightly inconsistent though, as the
> combination of both does not work: func()[0]() will result in a syntax
> error. Same applies to method array dereferencing. Is this
> intentional?
>
> 3. The new expression method, property and array dereferencing
> syntaxes were added in expr_without_variable. I am not sure whether
> this was intentional or not. That way (new A)->b will not be a
> variable (in the parser sense), whereas $a->b will. What is the impact
> of this? One thing that will not work is doing (new A)->b = 'c'. I
> know, this looks fairly stupid, but if you consider that A might
> overload the __set operation it might actually make sense. (It won't
> make much sense, but hey, (new A)[0] doesn't make much sense to me
> either). Other areas that might be affected are return by reference,
> pass by reference and iterate by reference (but I didn't test this).
>
> So, I know, those are all edge cases, so leaving them as is won't hurt
> much. Still I think it's better to discuss them ;)
>
> Nikita

---

## #56669 — devis@lucato.it — 2011-11-30T00:49:36+00:00

URL: https://externals.io/message/56441#56669 · In reply to: #56450

Hi,

is the new dereferencing syntax definitely part of 5.4 ?

Will it be possible to replace this:

$model = new DomainModel('foo', 'bar');
$result = $model->findByName('John');

with:

$result = (new DomainModel('foo', 'bar'))->findByName('John');

?


Thank you
Devis



> Amateurish question to internals:
> Why can not we do more general cases? For example (expr)[3];
> (expr)->foo->bar;
> (expr)()->baz[3]()->makeSomething()[4] = 16; etc.
>
> Now we have (new some)->something; and doSomething()[12], but why only
this
> cases?
>
> Big trouble in parser to do this? Can someone help me to find discussions
> about it, if they exist.
>
> With regards, Alexander Moskaliov
> Irker@irker.net
>
>
> 2011/11/20 Nikita Popov <nikita.ppv@googlemail.com>
>
> > […]

---

## #56670 — Rasmus Lerdorf — 2011-11-30T00:53:11+00:00

URL: https://externals.io/message/56441#56670 · In reply to: #56669

> Hi,
> 
> is the new dereferencing syntax definitely part of 5.4 ?
> 
> Will it be possible to replace this:
> 
> $model = new DomainModel('foo', 'bar');
> $result = $model->findByName('John');
> 
> with:
> 
> $result = (new DomainModel('foo', 'bar'))->findByName('John');
> 
> ?

Why don't you try it?  Download the latest RC from qa.php.net
./configure && make && make test (and send us the results)
and then you can test your code snippet.

-Rasmus

---

## #56671 — devis@lucato.it — 2011-11-30T01:22:05+00:00

URL: https://externals.io/message/56441#56671 · In reply to: #56670

Hi Rasmus,

I already tested a similar example (see http://codepad.viper-7.com/tNkTTX)
but I'm getting

> *Parse error*: syntax error, unexpected '->' (T_OBJECT_OPERATOR)

so my question... I thought this feature was part of 5.4 considering the
previous messages, perhaps it is not.

Devis




> 
> > […]
>
> Why don't you try it?  Download the latest RC from qa.php.net
> ./configure && make && make test (and send us the results)
> and then you can test your code snippet.
>
> -Rasmus

---

## #56673 — Rasmus Lerdorf — 2011-11-30T01:55:27+00:00

URL: https://externals.io/message/56441#56673 · In reply to: #56671

> Hi Rasmus,
> 
> I already tested a similar example (see
> http://codepad.viper-7.com/tNkTTX) but I'm getting 

Sorry, pasting something into codepad doesn't qualify as testing. You
have no idea how and when they built the 5.4 they are running. Until the
first release, 5.4 is a moving target. And everyone who posts to
internals are expected to be able to download and install 5.4 on their own.

But to answer your question, yes, that syntax will work fine. It is
right out of the UPGRADING file at:

http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/UPGRADING?view=markup

which says:

- Class member access on instantiation:
	  (new foo)->method()
	  (new foo)->property
	  (new foo)[0]

-Rasmus

---

## #56674 — Morfi — 2011-11-30T03:55:23+00:00

URL: https://externals.io/message/56441#56674 · In reply to: #56673

Why not possible?

$result = ($model = new DomainModel('foo', 'bar'))->findByName('John');




> 
> > […]
>
> Sorry, pasting something into codepad doesn't qualify as testing. You
> have no idea how and when they built the 5.4 they are running. Until the
> first release, 5.4 is a moving target. And everyone who posts to
> internals are expected to be able to download and install 5.4 on their own.
>
> But to answer your question, yes, that syntax will work fine. It is
> right out of the UPGRADING file at:
>
>
> http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/UPGRADING?view=markup
>
> which says:
>
> - Class member access on instantiation:
>          (new foo)->method()
>          (new foo)->property
>          (new foo)[0]
>
> -Rasmus

---

## #56675 — irker@irker.net — 2011-11-30T05:28:00+00:00

URL: https://externals.io/message/56441#56675 · In reply to: #56674

Morfi, see my mail in this thread. Still no answer to this question.
With regards, Alexander Moskaliov
Irker@irker.net


2011/11/30 Morfi <i@morfi.ru>

> Why not possible?
>
> $result = ($model = new DomainModel('foo', 'bar'))->findByName('John');

---

## #56685 — Nikita Popov — 2011-11-30T18:55:08+00:00

URL: https://externals.io/message/56441#56685 · In reply to: #56675

If I could get some comments on the original issues I mentioned (in
http://markmail.org/message/aohq5lcoeaiqos7y?q=php+internals+list:net.php.lists.internals)
it would be really nice, too :)

Nikita


> Morfi, see my mail in this thread. Still no answer to this question.
> With regards, Alexander Moskaliov
> Irker@irker.net
>
>
> 2011/11/30 Morfi <i@morfi.ru>
>
> > […]
