note 67567 deleted from function.implode by bjori
| From: | bjori@php.net | Date: | Thu, 08 Jun 2006 20:42:21 +0000 |
| Subject: | note 67567 deleted from function.implode by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-113914@lists.php.net to get a copy of this message | ||
Note Submitter: kamil at kube dot org
Reason: useless
----
Got class instances stored in an array? Implode them using implode_class_member:
<?
class FooBar {
private $iFoo;
private $sBar;
function getFoo() { return $this->iFoo; }
function getBar() { return $this->sBar; }
public function __construct( $iFoo, $sBar ) {
$this->iFoo = $iFoo;
$this->sBar = $sBar;
}
}
function implode_class_member( $sGlue, $aClasses, $sAccessorName ) {
if( !is_array( $aClasses ) || !count( $aClasses ) ) {
return NULL;
}
$aFoo = array();
foreach( $aClasses as $aClass ) {
$aFoo[] = eval( 'return $aClass->'.$sAccessorName.'();' );
}
return
implode( $sGlue, $aFoo );
}
$aCoo = array(
new FooBar( 1, 'foo' ),
new FooBar( 2, 'bar' ),
new FooBar( 9, 'coo' )
);
echo implode_class_member( ', ', $aCoo, 'getFoo' );
echo implode_class_member( ', ', $aCoo, 'getBar' );
?>