Aureole Posted October 11, 2007 Share Posted October 11, 2007 I'm trying to learn a bit of OOP... Let's say I have this: <?php class message { var $hello; function defineMessage($something) { $this->hello = $something; } function showMessage() { echo $this->hello; } } $message = new message; $message->defineMessage('Hello World.'); $message->showMessage(); ?> Well that works fine but I've been trying to work out how to make it so I'll use $something->message->defineMsg(); and $something->message->showMsg(); I guess it's a Class in a Class... So I can have like... $swr3->db->connect(); ...rather than $swr3->connect(); or $db->connect(); Thanks a lot. Link to comment https://forums.phpfreaks.com/topic/72758-solved-class-within-a-class/ Share on other sites More sharing options...
Aureole Posted October 11, 2007 Author Share Posted October 11, 2007 <?php class message { function definemsg($something) { $this->hello = $something; } function showmsg() { echo $this->hello; } } $swr3->message = new message; $swr3->message->definemsg('Hello World.'); $swr3->message->showmsg(); ?> This seems to work but the problem is, say if I have another class called bob I want $swr3 to still be used to get to the bob class... i.e. if I pretend I know what I'm doing for a second here's what I'd do... <?php class message { function defineMsg($something) { $this->hello = $something; } function showMsg() { echo $this->hello; } } class bob { function doStuff() { // Do stuff... } } $swr3->message = new message; $swr3->bob = new bob; $swr3->message->defineMsg('Hello World.'); $swr3->message->showMsg(); $swr3->bob->doStuff(); ?> EDIT: Lol that actually does work, yeay! Link to comment https://forums.phpfreaks.com/topic/72758-solved-class-within-a-class/#findComment-366926 Share on other sites More sharing options...
coder_ Posted October 11, 2007 Share Posted October 11, 2007 Read the manual!!!!!!!!! And read it now!!!! Link to comment https://forums.phpfreaks.com/topic/72758-solved-class-within-a-class/#findComment-366939 Share on other sites More sharing options...
Aureole Posted October 11, 2007 Author Share Posted October 11, 2007 I'd be 83 by the time I finished reading it. I do take a look at it sometimes but yeah... if everyone read the manual instead of posting this Forum would be dead. Link to comment https://forums.phpfreaks.com/topic/72758-solved-class-within-a-class/#findComment-366949 Share on other sites More sharing options...
coder_ Posted October 11, 2007 Share Posted October 11, 2007 I'd be 83 by the time I finished reading it. I do take a look at it sometimes but yeah... if everyone read the manual instead of posting this Forum would be dead. That is not true. 1. Learn basics. 2. If you have problem - google it. 3. can't find solution to your problem - post on forum. You should do first step. Link to comment https://forums.phpfreaks.com/topic/72758-solved-class-within-a-class/#findComment-366963 Share on other sites More sharing options...
Aureole Posted October 11, 2007 Author Share Posted October 11, 2007 I was joking hence the smilies, for your information I was looking at the manual and I did search Google. I always Google before posting here... well most of the time. Link to comment https://forums.phpfreaks.com/topic/72758-solved-class-within-a-class/#findComment-366990 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.