note 70044 added to function.func-get-args

From: Date: Sun, 01 Oct 2006 13:02:25 +0000
Subject: note 70044 added to function.func-get-args
Groups: php.notes 
Request: Send a blank email to php-notes+get-117895@lists.php.net to get a copy of this message
To pass named arguments in a Perl fashion through class constructors, I use this: <?php class Test{ // set class defaults for values not assigned by constructor public $a = 0; public $b = 'string'; public $c = array(); public function __construct( $args = array() ) { // parse tagged arguments in Perl fashion 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; } } } } $t = new Test( array( 'b'=>'new value', 'c'=>array(1,'test') ) ); ?> This allows $a to keep its default of 0, while $b gets reassigned to 'new value' and $c becomes the array(1,'test'). The catch is that you add O(n^2) "big-O notation" to the begging of every class constructor which becomes expensive on "larger" classes. While arguments defaults like the following have only O(n) "constant" amount of work. <?php public funciton __construct( $a=0, $b='string', $c=array()){ ... } ?> ---- Server IP: 216.194.113.175 Probable Submitter: 208.132.15.204 ---- Manual Page -- http://www.php.net/manual/en/function.func-get-args.php Edit -- https://master.php.net/note/edit/70044 Del: integrated -- https://master.php.net/note/delete/70044/integrated Del: useless -- https://master.php.net/note/delete/70044/useless Del: bad code -- https://master.php.net/note/delete/70044/bad+code Del: spam -- https://master.php.net/note/delete/70044/spam Del: non-english -- https://master.php.net/note/delete/70044/non-english Del: in docs -- https://master.php.net/note/delete/70044/in+docs Del: other reasons-- https://master.php.net/note/delete/70044 Reject -- https://master.php.net/note/reject/70044 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#117895) next »