Jump to content

[SOLVED] Class within a Class?


Aureole

Recommended Posts

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
Share on other sites

<?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
Share on other sites

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.  ;)  ;D

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.