[UPHPU] Accessing an element in an array returned from a function
Walt Haas
haas at xmission.com
Fri Jan 30 08:51:29 MST 2009
Richard K Miller wrote:
> If abc() returns an array, is there any possible notation that lets me
> access a single element?
>
> echo abc()[0]; // I wish this were possible
>
> ...instead of having to use an intermediate variable?
>
> $temp = abc(); // $temp is ugly
PHP 5.2 does seem to support the so-called "fluent programming" style:
<?php
class Order
{
private $itemList;
public function __construct()
{
$this->itemList = array();
}
public function addItem($arg)
{
$this->itemList[] = $arg;
return $this;
}
public function printItems()
{
foreach( (array)$this->itemList as $item ) {
print " $item\n";
}
}
}
$order = new Order;
$order->addItem('something')
->addItem('anotherthing')
->addItem('something else')
->printItems();
More information about the UPHPU
mailing list