问题
In PHP, the following code is valid
$a=array(0);$a[0];
but that one is invalid:
array(0)[0]
- What is the terminology corresponding to that behaviour? (has it anything to do with "dereferencing"?)
- 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)
回答1:
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 :
- RFC - Function Array Dereferencing
- Features in PHP trunk: Array dereferencing, when it was unsure whether there would be a PHP 5.4 or a PHP 6
- And, last but not least, the (currently last) news on php.net : PHP 5.4 alpha1 released
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.
回答2:
This is called "array dereferencing" and it will be available for use in PHP5.4.
来源:https://stackoverflow.com/questions/6800464/terminology-question-on-dereferencing