D_C wrote:
Hiya -

I want to create a class that inherits from a database object.

eg I have my own "user" Class, with general methods that do calculations etc.
However, it also reflects a table of the database, eg "age, location".

I want some methods called on the object to be handled by my class, but other

so at constructor time, something like

$this->parent = mysql_fetch_object($res); // returns a mysql obj with
age, loc etc records

then ...

$age = $userObj->age;   // this actually comes straight from the dbase obj
$rating = $userObj->rating;   // a public var calculated at construction
$life = $userObj->getSpend();  // this is a method to do a calc.

I could use a component (is the right OOP pattern word?)
$age = $userObj->dbObj->age


but I wanted to hide where the items are coming from. Also i want to
be able to just keep adding to the database without having to create
gettter/settter functions for everything...

I think python had some nice means of doing get/set functions
transparently, does PHP5 have anything like this?


very basically, add a __get() function to your User class.
(or its base class or whatever):

        function __get($var)
        {
                // $userObj->dbObj should be protected/private
                if (isset($this->dbObj->$var)) {
                        return $this->dbObj->$var;
                }               
        }

if you now do:

echo $userObj->rating;
echo $userObj->age;

this will output as if you had called:

echo $userObj->dbObj->rating;
echo $userObj->dbObj->age;

assuming that rating and age don't exist as public properties in $userObj,
and do exist in $userObj->dbObj.


thanks!

/dc

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to