note 70853 modified in function.func-get-args by danbrown
| From: | danbrown@php.net | Date: | Sat, 19 Jul 2008 21:45:44 +0000 |
| Subject: | note 70853 modified in function.func-get-args by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141764@lists.php.net to get a copy of this message | ||
Same idea as below, but this:
<?php
foreach( $args as $k => $v ){
switch($k){
case 'a':
$this->a= $v; break;
case 'b':
$this->b= $v; break;
case 'c':
$this->c= $v; break;
}
}
?>
can be shortened to this (as long as all public variables have default values set in their
declarations):
<?php
foreach( $args as $k=>$v)
if(isset($this->$k)) $this->$k = $v;
?>
--was--
Same idea as below, but this:
foreach( $args as $k => $v ){
switch($k){
case 'a':
$this->a= $v; break;
case 'b':
$this->b= $v; break;
case 'c':
$this->c= $v; break;
}
}
can be shortened to this (as long as all public variables have default values set in their
declarations):
foreach( $args as $k=>$v)
if(isset($this->$k)) $this->$k = $v;
http://php.net/manual/en/function.func-get-args.php