1

In PHP, the following code is valid

$a=array(0);$a[0];

but that one is invalid:

array(0)[0]
  1. What is the terminology corresponding to that behaviour? (has it anything to do with "dereferencing"?)
  2. What is the motivation behind such a behaviour (besides user spite :-P)

I am looking for the general terminology, not necessarily the terminology associated with PHP.

(Other example: in MATLAB, the following is valid:

s = size(M)
s(0)

but that is invalid:

size(M)(0)

In both PHP and MATLAB, adding parenthesis does not help, i.e., (array(0))[0] and (size(M))(0) are both invalid)

Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90
  • Actually, although `size(M)(0)` is not valid in Matlab, it is valid in Octave (GNU alternative for Matlab). Simple Google search implies that 'array dereferencing' is terminology heavily related to `php` and `perl` (and `c/ c++` pointer dereferencing). I have always associated dereferencing just to pointers. So I don't know how general terminology dereferencing really is among all the languages. – eat Jul 23 '11 at 14:37

2 Answers2

3

That's called Array dereferencing, and will become available in PHP 5.4 (which is currently in alpha)

Note (thanks Gordon) : what you are asking for, with array()1, is not possible even in PHP 5.4 -- but it'll work for functions.


A couple of sources :


Quoting that last news :

Here is an incomplete list of changes:
- Added: Traits language construct
- Added: Array dereferencing support
- Added: DTrace support
- Improved: Improved Zend Engine memory usage and performance
- Moved: ext/sqlite moved to pecl (sqlite3 support is still built-in)


1.array() is not a function, even if it looks like one -- it's actually what PHP calls a language construct ; and those don't behave like functions.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 1
    @Gordon : true, I've edited my answer to point out that this will work for **functions** -- might re-edit to insist on the fact that `array()` is not a function, though ; thanks for your comment :-) – Pascal MARTIN Jul 23 '11 at 12:58
1

This is called "array dereferencing" and it will be available for use in PHP5.4.

Gordon
  • 312,688
  • 75
  • 539
  • 559
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • I'm looking for the general idea. Is it the same terminology for the MATLAB behaviour? And why was it not available in php 1.0 in the first place? That seems so natural... – Olivier Verdier Jul 23 '11 at 12:53
  • @Olivier PHP was missing a lot of obvious features over the years. It's gradually shifted from a very web-oriented language to a full-featured, general purpose language. – Michael Berkowski Jul 23 '11 at 12:57
  • @Oliver: Why was it not available in PHP 1.0? Because the general idea was to return what was wanted (i.e. not an array if only a single value is desired). Why is it available now? Sometimes we give in to peer pressure and add things that "the masses" request. – salathe Jul 23 '11 at 14:47