You are here: Re: [PHP] inheriting from non similar types? « PHP « IT news, forums, messages
Re: [PHP] inheriting from non similar types?

Posted by Jochem Maas on 02/14/05 03:13

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация