note 108442 added to function.func-get-args

From: Date: Thu, 26 Apr 2012 18:10:59 +0000
Subject: note 108442 added to function.func-get-args
Groups: php.notes 
Request: Send a blank email to php-notes+get-187364@lists.php.net to get a copy of this message
In this example, we try to show how to assign the value of the arguments for the variables dynamically and why only works this way. We will see how to work with get_class_vars () and func_get_args (). We can see that when assign to the class variables to an array that keys of this array are of type string, for it the array keys are the names of variables, thus we could not use a numeric index to access elements of this array. To enable access to these elements we must first transform the array keys in numeric index keys, then we can work with the use of indexes. <? // Creates a Class class foo { // Define varibles private $a='01', $b='02', $c='03'; // Creates the constructor with a single required argument function __construct($a) { // Sets the argument value to variable $this->a=$a; // Count how many arguments were passed $nArgs = func_num_args(); // Run if number of arguments is greater than one if($nArgs>1) { // Creates an array with the class variables $cVars = get_class_vars(__CLASS__); // The array keys are the name of class variables var_dump($cVars); // array(3) { ["a"]=> string(2) "01" ["b"]=> string(2) "02" ["c"]=> string(2) "03" } // Show value of array elements echo '<br> => '.$cVars[0]; // => null echo '<br> => '.$cVars[a]; // => 01 // Show value of class variables by reference of $this echo '<br> => '.$this->{1}; // => null echo '<br> => '.$this->{b}; // => 02 // Show value by reference $this with array $cVars echo '<br> => '.$this->$cVars[2]; // => Show a error: "Fatal error: Cannot access empty property in C:\..." Because this array element not exist echo '<br> => '.$this->$cVars['a']; // => null // Because "$cvars[a]" was set to a value and not a reference to class variable // Changes the array, the values given the keys and the keys change to numeric index $cVars = array_keys($cVars); // Now, the array elements are a reference to the class variable and can be accessed by an index echo '<br><br>'; var_dump($cVars); // array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" } // First prints the default value of the class variables and then the value assigned by the argument reset($cVars); for($i=1; $i<$nArgs; $i++) { $str .= '<br> Index:'.$i.', key ['.current($cVars).'] => '.$this->$cVars[$i].', Arg('.$i.') == '.func_get_arg($i); $this->$cVars[$i]=func_get_arg($i); $str .= '<br> Index:'.$i.', key['.current($cVars).'] => '.$this->$cVars[$i].', Arg('.$i.') == '.func_get_arg($i); next($cVars); } echo '<br>'.nl2br($str).'<br><br>'; } print_r($this); } } new foo('alpha',12345,'charlie'); ?> Now we can see below the same content over briefly. <? class test { private $a='01', $b='02', $c='03'; function __construct($a) { $this->a=$a; $nArgs = func_num_args(); if($nArgs>1) { $cVars = get_class_vars(__CLASS__); $cVars = array_keys($cVars); for($i=1; $i<$nArgs; $i++) $this->$cVars[$i]=func_get_arg($i); echo $this->a.', '.$this->b.', '.$this->c; } } } $foo = new test('alpha',12345,'charlie'); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 201.76.70.21 ---- Manual Page -- http://www.php.net/manual/en/function.func-get-args.php Edit -- https://master.php.net/note/edit/108442 Del: integrated -- https://master.php.net/note/delete/108442/integrated Del: useless -- https://master.php.net/note/delete/108442/useless Del: bad code -- https://master.php.net/note/delete/108442/bad+code Del: spam -- https://master.php.net/note/delete/108442/spam Del: non-english -- https://master.php.net/note/delete/108442/non-english Del: in docs -- https://master.php.net/note/delete/108442/in+docs Del: other reasons-- https://master.php.net/note/delete/108442 Reject -- https://master.php.net/note/reject/108442 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#187364) next »