$this->object->do();

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

$this->object->do();

Post by Ree »

Code: Select all

$this->object->doSomething();
Is this kind of expression all right? I used this in a constructor of one of my classes. The class needs to use my database class to access/modify data in the db, so I used

Code: Select all

$this->db->new Database(db_host, db_name, db_user, db_pass);
$this->db->Connect();
in the class' constructor, which enabled me to use the same connection while using different functions instead of recreating the db object every time in those functions. The thing did work, but still, do you find this normal or should I rethink something?
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Re: $this->object->do();

Post by harrisonad »

Yes, this is ok.

But I think you didn't recognized the syntax of you post.

Code: Select all

$this->db = new Database(db_host, db_name, db_user, db_pass);
$this->db->Connect();
I changed -> to = on line 1
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Oh yes, that was my mistake. Thanks
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Code: Select all

$this->classFunction->doWhatever()
is called composition, a commonly used OO-technique. It's counterpart is aggregation (see same article).
Post Reply