note 53481 deleted from language.oop5.overloading by nlopess
| From: | nlopess@php.net | Date: | Fri, 03 Jun 2005 15:00:33 +0000 |
| Subject: | note 53481 deleted from language.oop5.overloading by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-89866@lists.php.net to get a copy of this message | ||
Note Submitter: general_vini at hotmail dot com
----
<?php
##______________________________________
##______________________________________
## AND NOW YOU CAN USE SET PROPERTY,I CUT SOME COMMENTS
##______________________________________
##______________________________________
##______________________________________
##______________________________________
#THANKS FOR YOUR EXAMPLE mileskeaton
##______________________________________
##______________________________________
class Person
{
## this top stuff is just an example. could be anything.
private $name;
private $age;
private $weight;
function __construct($name, $age, $weight)
{
$this->name = $name;
$this->age = $age;
$this->weight = $weight;
}
## PORTABLE: use this __call function in any class
function __call($val, $x)
{
# the propertys now can only be call like 'get_varname' and
'set_varname', its more formal
$op = substr($val, 0, 4);
if ( ( $op == 'get_') || ($op == 'set_') )
$varname = strtolower(substr($val, 4));
else
die("method $val does not exist\n");
# now see if that variable exists:
foreach($this as $class_var=>$class_var_value)
{
if(strtolower($class_var) == $varname)
{
if ( $op=='get_')
return $class_var_value;
else
$this->$varname = $x[0];
}
} // END OF FOREACH
}// END OF CALL
} // END OF CLASS
$a = new Person('Miles', 35, 200);
print "<pre>";
print $a->get_name() . "\n";
$a->set_AGE(1500);
print $a->get_age() . "\n";
$a->set_Weight(500);
print $a->get_Weight() . "\n";
print "</pre>";
?>