[nycphp-talk] Access an element of a method that returns an ar ray
David Sklar
sklar at sklar.com
Wed Jul 21 09:44:05 EDT 2004
While you can do that nice chaining with object methods in PHP 5, you
can't do it with array indices.
This is OK in PHP 5:
<?php
class Stooge {
private $name = null;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
function get_object() {
return new Stooge('Moe');
}
print get_object()->getName();
?>
It prints "Moe". But this is not:
<?php
function get_array() {
return array('Moe','Larry','Curly');
}
// these all are parse errors:
print get_array()[1];
print (get_array())[1];
print {get_array()}[1];
?>
David
Joe Crawford wrote:
> Scott,
>
> thanks for this clarification that is a feature of 5 that i did not know
> about yet ;)
>
> Joe Crawford Jr.
>
> On Wed, 2004-07-21 at 07:54, Scott Mattocks wrote:
>
>>Joe Crawford wrote:
>>
>>
>>>i dont see this working in 4 or 5 reason being is you have to have the
>>>method return the array before you can actually access the elemnets so
>>>$obj->method()[3] will not work you must assign it to a variable before
>>>you can access it.
>>
>>PHP 4 doesn't know what to do with this syntax but one of the changes in
>>PHP 5 is to let you use the return value of a function without assigning
>>it first. If your function/method returns an object you can call
>>methods of that object without assigning it to a variable first.
>>Ex: $obj->getOtherObj()->method();
>>
>>Scott Mattocks
>>_______________________________________________
>>talk mailing list
>>talk at lists.nyphp.org
>>http://lists.nyphp.org/mailman/listinfo/talk
>>
>>
>
>
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
>
More information about the talk
mailing list