thanks in advance
here is the code
<?php
$line="<br />\n";
class Person
{ // class person start
var $name, $age;
static $salary;
function __construct($name, $age,$salary)
{ //construct start
$this->set( "name",$name);
$this->set( "age",$age);
self::sset("salary",$salary);
} // construct end
function set($property,$value)
{// setter start
$this->$property=$value;
}//setter end
function get($property)
{//getter start
return $this->$property;
}//getter end
function sset($property, $value)
{//static setter start
self::$$property=$value;
}// static setter end
function sget($property)
{//static getter start
return self::$$property;
}//static getter end
}// class person end
$x=new Person("hatem",35,10000);
$y=new Person("atif",40,12000);
// printing x values
echo "x.name: ".$x->get("name").$line;
echo "x.age: ".$x->get("age").$line;
echo "x.salary: ".$x->sget("salary").$line;
//printing y values
echo "y.name: ".$y->get("name").$line;
echo "y.age: ".$y->get("age").$line;
echo "y.salary: ".$y->sget("salary").$line;
?>
Yes, although I think in 99.9% of cases it's a bad idea.
Look into the __dict__ attribute. Each instance object has one, and
it's a dictionary of all of the user-defined attributes. For example:
class Person(object):
def __init__(self, name):
self.name = name
def report(self):
print "name: " + self.__dict__["name"]
You could write little get() and set() functions that just manipulate
the __dict__, but there's really no reason to. For example:
def set(self, attr, value):
self.__dict__[attr] = value
and then you could write:
x = Person("Shawn")
x.set("name", "Hatem")
but that's the same as writing:
x.__dict__["name"] = "Hatem"
or even just:
x.name = "Hatem"
You might want to look into not using a class at all if you need to do
a lot of this sort of "dynamic" instance access. Just use a
dictionary and write some functions to manipulate your dictionaries.
You can use modules for scoping.
--
Shawn
--
You received this message because you are subscribed to the Google Groups "Utah Python User Group" group.
To post to this group, send email to utahp...@googlegroups.com.
To unsubscribe from this group, send email to utahpython+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/utahpython?hl=en.
>> utahpython+...@googlegroups.com<utahpython%2Bunsu...@googlegroups.com>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/utahpython?hl=en.
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Utah Python User Group" group.
> To post to this group, send email to utahp...@googlegroups.com.
> To unsubscribe from this group, send email to
> utahpython+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/utahpython?hl=en.
>
>
--
the blendmaster