You are here: Re: Objects or not ? « PHP Programming Language « IT news, forums, messages
Re: Objects or not ?

Posted by ZeldorBlat on 11/13/07 15:41

On Nov 13, 8:23 am, floort...@gmail.com wrote:
> Hi,
>
> Im having a discussion with a friend about Objects.
>
> Let say I create a user Object
>
> class User
> {
> private $_aUserdata;
> rivate $_sId;
>
> function __construct($id)
> {
> $this->_sId=$id;
> $this->_aUserdata= db::select('SELECT * FROM users WHERE UserId ="'.
> $id.'"');
>
> }
>
> public function getName()
> {
> return $this->_aUserdata['firstnamename'].' '.$this-
>
> >_aUserdata['lastname'];
> }
>
> function __destruct()
> {
> // just for this example
> echo 'UPDATING USER';
> $db::update('UPDATE users SET' lastsused="NOW()" WHERE UserId="'.$this-
>
> >_sId.'"' );
> }
> }
>
> Now i can have easy access to the users
>
> $user=new User(1);
> echo $user->getName();
>
> // Arjen
> // UPDATING USER
>
> But no script I ever write is this simple.
> Lets says that I want to select all forum entries and display the user
> name along with the other data
>
> $forumdata=$db::select('SELECT * FROM forum INNER JOIN USERS on
> forum.UserId=users.UserId');
>
> Now my userclass is completely useless because I allready have all the
> data I need and I dont want to do another xxx select queries to the
> user database. However i would still like to use User::getName()
> because I might do some fancy calculation there and i dont want to
> multpily my code. I now also dont have access to (the complpetely
> useless) destructor so I have to update this manually.
>
> How would I go and solve such a problem ?
>
> I could imagine that I could loop through the forumdata and load the
> user data into a new user object but is that really a good
> solution ?
>
> Arjen

There are lot's of different ways to do this. I typically create
static "factory" methods to create the objects rather than call the
constructor directly. For instance, your constructor can look like
this:

//Protected so you have to use the factory methods
protected function __construct($id, array $userData) {
$this->_sId = $id;
$this->_aUserdata = $userData;
}

Now you create a factory method called "fromID" like this:

public static function fromID($id) {
$class = __CLASS__;
$u = new $class($id, db::select('SELECT * FROM users WHERE UserId
="'.$id.'"'));
return $u;
}

And you can create a factory method called "fromArray" like this:

public static function fromArray(array $a) {
$class = __CLASS__;

$r = array();
foreach($a as $row)
$r[] = new $class($row['id'], $row);

return $r;
}

So, for a single id you can get your object like this:

$user = User::fromID(42);

And, if you already have the result of a query in an array, you can
get an array of user objects without re-running your query like this:

$forumdata = $db::select('SELECT * FROM forum INNER JOIN USERS on
forum.UserId=users.UserId');
$users = User::fromArray($forumdata);

 

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

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