|
Posted by Jerry Stuckle on 11/13/07 13:40
floortje4@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
>
>
First of all, don't do the select in your code. Let your objects do it.
In this case, create a userlist class and let it perform the select
statement, building a new userobject for each row.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|