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
https://forums.phpfreaks.com/topic/72758-solved-class-within-a-class/
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!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.