note 71040 deleted from language.oop5 by colder
| From: | colder@php.net | Date: | Sun, 19 Aug 2007 23:36:29 +0000 |
| Subject: | note 71040 deleted from language.oop5 by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-129601@lists.php.net to get a copy of this message | ||
Note Submitter: phaxius
----
Thanks for the tip Dave, that is a much simpler way to the same functionality. One thing I noticed
is that if you want to validate the arguments passed to __construct() but also are using
"$this->Args = func_get_args();" to define the class members then you get all or none
of the arguments stored as members after validation. For an example, a class that stores a list of
numbers may require validation of the arguments to ensure that they are actually numbers before
storing this in the object.
Using "$this->Arg = func_get_args();" you could write something like:
<?php
class numbers
{
public function __construct(){
if(func_num_args()>=1){ // if there is at least one argument
for($i=0; $i<func_num_args(); $i++)
{ // validation loop
if(!is_numeric(func_get_arg($i))){ // if not a number
echo 'Invalid argument <br />'; // complain
// return false; // keeps _construct() from defining
} // object members
}
$this->Arg=func_get_args(); // defines object members
} else {die('No arguments passed');} // stops if nothing is
} // passed to _construct()
public function addNums(){
$sum = 0; // place to store the sum, initialized to 0
foreach($this->Arg as $number){ // addition loop
$sum = $sum + $number; // add the previous sum with the
} // value of the current position in the array
return $sum; // returns the final sum
}
public function __destruct(){}
}
$numbers= new numbers(2.3, 7, 62, 3.75, 82, 3, 'n');
foreach($numbers->Arg as $number){
echo $number . '<br />';
}
echo $numbers->addNums() . '<br />';
?>
which would output:
Invalid argument
2.3
7
62
3.75
82
3
n
160.05
Although the sum is correct in this case, the storage of invalid data can cause issues on more
complex code, such as if you pass objects in the construction of other objects. If the argument
turns out to be an invalid object, and it stores it anyways, some functions involving members of
that object will malfunction later. An example of what might happen can be seen if you uncomment the
line of code that says "return false;", which would then output:
Invalid argument
Warning: Invalid argument supplied for foreach() in path\filename.php on line 27 Warning: Invalid
argument supplied for foreach() in path\filename.php on line 18 0
In this case it results from a lack of member definition within the numbers class itself, but if
something is supposed to be an object of a specific type and it is improperly validated then similar
messages would result from trying to access the members of that object since it doesn't exist.
As an alternative, one may wish to instead assign default values if something doesn't check out
right. That way you can still use the information provided without causing error messages if
something is input incorrectly. The code for the alternative constructor could look like:
public function __construct(){
if(func_num_args()>=1){ // if there is at least one argument
for($i=0; $i<func_num_args(); $i++){ // validation loop
if(is_numeric(func_get_arg($i))){ // if the arg is numeric
$nums[$i]=func_get_arg($i); // store it in $nums[$i]
} else {$nums[$i]=0;} // otherwise set to default
$this->Arg[$i]=$nums[$i]; // defines the object members
}
} else {die('No arguments passed');} // stops if nothing is
} // passed to _construct()
which if used would output:
2.3
7
62
3.75
82
3
0
160.05
The invalid data type was set to a default value that is the correct type. One could also add code
to clean it up so that it never shows up in the final object.