note 53481 added to language.oop5.overloading
| From: | general_vini at hotmail dot com | Date: | Thu, 02 Jun 2005 19:32:50 +0000 |
| Subject: | note 53481 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89830@lists.php.net to get a copy of this message | ||
<?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>";
?>
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53481
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53481&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53481&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53481&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53481&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53481&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53481&report=yes
Search -- http://master.php.net/manage/user-notes.php