note 85343 added to function.func-get-args

From: Date: Tue, 26 Aug 2008 21:25:23 +0000
Subject: note 85343 added to function.func-get-args
Groups: php.notes 
Request: Send a blank email to php-notes+get-143847@lists.php.net to get a copy of this message
Instead of having to define your arg list twice, and keeping to the good style of initialising your variables in the head of your class, you can use (PHP5): <?php class myclass { public $value = null; public $key = null; public $column = null; public $table = null; public function __construct() { $vars = get_class_vars(); for($i=0; $i<func_num_args();$i++) { $this->${$vars[$i]}=func_get_arg($i); } } } ?> which should allow you to set variables while retaining their default values if they are not set (in this case, null), without having to mess around with functions to retain default values so is much neater (just don't change the order you declare your vars!) <?php //usage $c = new myclass("value", "tablekey", "tablecol", "table"); echo $c->key; //prints 'tablekey' ?> ---- Server IP: 87.124.64.96 Probable Submitter: 82.30.102.138 ---- Manual Page -- http://www.php.net/manual/en/function.func-get-args.php Edit -- https://master.php.net/note/edit/85343 Del: integrated -- https://master.php.net/note/delete/85343/integrated Del: useless -- https://master.php.net/note/delete/85343/useless Del: bad code -- https://master.php.net/note/delete/85343/bad+code Del: spam -- https://master.php.net/note/delete/85343/spam Del: non-english -- https://master.php.net/note/delete/85343/non-english Del: in docs -- https://master.php.net/note/delete/85343/in+docs Del: other reasons-- https://master.php.net/note/delete/85343 Reject -- https://master.php.net/note/reject/85343 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143847) next »