note 57985 added to language.oop
| From: | KarimAhmed at osu1 dot php dot net | Date: | Thu, 20 Oct 2005 19:46:49 +0000 |
| Subject: | note 57985 added to language.oop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97319@lists.php.net to get a copy of this message | ||
This class method allows you to take a nice associative array of parameters into your class when the
number of parameters gets unmanagable and/or has lots of default-value parameters.
It is so useful I suggest putting on a general base class (I call mine Object) so all your
descendents can use it.
-----------------------------------------------------
Code:
-----------------------------------------------------
function setClassVars( &$parameter_array )
{
$class = get_class($this);
$class_vars = get_class_vars($class);
/*
* check that each of the passed parameters are valid before setting the
* appropriate class variable.
*/
foreach ( $parameter_array as $var => $value ){
if ( array_key_exists( $var, $class_vars ) ){
$this->$var = $value;
}
else{
$this->raiseError(
'setClassVars: class variable "' .
$var .
'" does not exist on class "' .
$class . '"'
);
}
}
}
-----------------------------------------------------
for example:
A complicated, messy, class method Before:
-----------------------------------------------------
render(
&$business_object,
$on_change=false,
$on_blur=false,
$options=false
$name
){
$this->business_object = $business_object;
$this->name = $name;
$this->on_blur = $on_blur;
$this->options = $options;
<code>
}
-----------------------------------------------------
A nice clean method After:
-----------------------------------------------------
render(
&$parameter_array
){
$this->setClassVars($parameter_array);
<code>
}
-----------------------------------------------------
Example call:
-----------------------------------------------------
$parameter_array =
array(
'business_object' => $business_object,
'name' => $field_name,
'on_change' => 'form.submit()',
'on_blur' => 'javascript:validate()',
$options =>
array(
'1' => 'None Selected',
'2' => 'Blue',
'3' => 'Red'
)
);
$my_object->render( &$parameters );
-----------------------------------------------------
Now isn't that easier to read AND maintain. Now you can change your API without the hassle of
amending every call to the method! You no longer have to worry about where the default params are or
even the order of your params.
I hope someone else who has been swearing at their code finds this useful ;-)
----
Manual Page -- http://www.php.net/manual/en/language.oop.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57985
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57985&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57985&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57985&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57985&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57985&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57985&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57985&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57985&report=yes
Search -- http://master.php.net/manage/user-notes.php