Page 1 of 1

Class within a Class... possible?

Posted: Fri Mar 12, 2004 9:04 am
by randomblink
Ok...
I have two classes.

dbConnect Class:
This is a very simple MySQL db class. It provides connection to a db and returns a variety of SQL Queries.

Mapp Class:
This class builds a very simple GRID map, for a game I play. It puts together a simple Table in HTML filled with Planets and Ships from this game.

INSIDE the Mapp Class, I have created an instance of the dbConnect class. This is causing problems. For instance, I created a Class Variable called $dbConnection, I generally set $dbConnection to:

$this->dbConnection( various args for db connection );

Then, inside the functions of my Mapp class I end up utilizing the dbConnect Class like this:

$this->dbConnection->select( $table, $args );

Should this work ok? I keep getting errors and I am not sure...
Is there a better way to do this?

HELP ME! If you can...

Posted: Fri Mar 12, 2004 9:48 am
by penguinboy
An example of an class within a class:
(untested, wrote it on the fly, so there is probably a parse error or two)

Code: Select all

<?php
class output
{
    var $input;

    function output()
      {$this->input = new input;}
    
    function enter_text($text)
      {$this->input->append_input($text);}

    function output()
      {return $this->input->get_input();}

    function get_input_log()
      {return $this->input->get_log();}
}

class input
{
    var $log;
    var $input;
    function input()
      {$this->clear_input();}

    function append_input($input)
    {
        $this->input .= $input."\n";
        $this->log .= $input."\n";
    }

    function clear_input()
      {$this->input = '';}

    function get_input()
    {
        $return = $this->input;
        $this->input->clear_input();
        return $return;
    }

    function get_log()
      {return $this->log;}
}


$output = new output;
$output->enter_text("This is some text 1.");
print '<pre>'.$output->output();
$output->enter_text("This is some text 2.");
$output->enter_text("This is some text 3.");
print '<pre>'.$output->output();
print '<pre>'.$output->get_input_log();
?>

Posted: Fri Mar 12, 2004 9:52 am
by patrikG
Should work - there isn't anything wrong with:

$this->dbConnection->select( $table, $args );

alternatively, you can simply access the function from the class you need without instantiating it.
Simple include the include-file (as if you were to use it to declare the object in the next line), then use :: instead of ->.

Say your DB-class looked a bit like this:

Code: Select all

<?php
////file myDBClass.inc.php
class my_db_class
{
function my_db_class
{}
functin select($table,$rows)
{
....
}
}

?>
Now to use this from your other class

Code: Select all

<?php
include_once("myDBClass.inc.php");
$result=my_db_class::select($table,$rows);
?>

My Thanks are overwhelming...

Posted: Fri Mar 12, 2004 10:18 am
by randomblink
Thanks alot you two...
I can't even begin to tell you how grateful I am...

This is my first foray into PHP and I am getting excited with all it could do, I just ASSUMED that it could do what I was wanting it to, but I keep getting weird errors... I will look around and see what else might be causing it now... Thanks again...