Page 1 of 1

Class Scope

Posted: Sun Jul 09, 2006 7:53 pm
by Benjamin
How do I access and call functions in a database class that I wrote from within a login class that I wrote?

Posted: Sun Jul 09, 2006 8:04 pm
by feyd

Code: Select all

class login
{
  var $dbInstance;

  function login()
  {
    $this->dbInstance =& new Database();
  }

  function foo()
  {
    $this->dbInstance->method(); // note: I don't remember if this works as expected in php 4 or not, but it does in 5. If it doesn't, make a reference to the database instance first, then go through that.
  }

  function foo2($dbInstance)
  {
    $dbInstance->method();
  }
}

Posted: Sun Jul 09, 2006 8:16 pm
by Benjamin
The database class is instantiated before the login class is though...

header.php

Code: Select all

$Database = new databaseConnection();
$Database->connect($host, $username, $password);
$Database->selectDatabase($database);

$Login = new login;
classes.php

Code: Select all

class login {
    function login() {
        $this->method   = "POST";
        $this->action   = "#";
        $this->name     = "LoginForm";
        $this->template = "includes/templates/login.php";
        $this->username = '';

        if ((isset($_POST['action'])) and ($_POST['action'] == 'Login')) {
            $this->validate();
        }
    }

    function validate() {
        if ((!isset($_POST['username'])) or ($_POST['username'] == '')) {
            $SystemStatus[] = 'Please enter your username.';
            return false;
        }
        if ((!isset($_POST['password'])) or ($_POST['password'] == '')) {
            $SystemStatus[] = 'Please enter your password.';
            return false;
        }
        $Database->table = 'members';
        $Database->parameters = array('username' => $_POST['username']);
        $Database->limit = '1';
        $Database->dispatch('select', 'user_status user_password');

        if ($Database->getNumRows() == 1) {

        } else {
            $SystemStatus[] = "Your username could not be found in the database.";
        }
    }

    function display() {
        include $this->template;
    }
}

class databaseConnection {
    function databaseConnection() {
        $this->database = '';
        $this->query = '';
        $this->fields = '';
        $this->table = '';
        $this->parameters = '';
        $this->limit = '';
        $this->orderby = '';
        $this->joins = '';
        $this->linkid = '';
        $this->connected = false;
        $this->dbselected = false;
    }

// etc. etc..

}

Posted: Sun Jul 09, 2006 8:20 pm
by feyd
soo, pass the database instance into the login class for its use like foo2() is written or through the constructor, similarly to foo2().

Posted: Sun Jul 09, 2006 8:23 pm
by Benjamin
Figured that out right before you posted. Thanks a lot for the help.

Posted: Sun Jul 09, 2006 8:49 pm
by Jenk
I use the constructor to create the instances for the DB object, having used Java before touching PHP this was a natural path to take..

Code: Select all

<?

class Foo
{
    private $db;

    public function __construct ($obj)
    {
        $this->db = $obj;
    }
}

class Bar
{}

$foo = new Foo(new Bar);

?>

Posted: Sun Jul 09, 2006 11:54 pm
by Benjamin
Could I have used

Code: Select all

$GLOBALS['Database']
rather than passing the $Database variable to the class.

Posted: Sun Jul 09, 2006 11:55 pm
by Benjamin
Jenk wrote:I use the constructor to create the instances for the DB object
Yeah, reading through the manual I started using __construct as well. I cannot re-instantiate the database class though as it's only initialized once per page because I want it to operate that way.

Posted: Mon Jul 10, 2006 12:03 am
by feyd
Jenk's doesn't recreate the object, just stores the reference.

Posted: Mon Jul 10, 2006 12:05 am
by Benjamin
Ok I see.

Posted: Mon Jul 10, 2006 12:45 am
by bdlang
It's just a slightly different method of composition. Rather than instantiating a database class object within your class, Jenk's example passes a new reference to a database object to the constructor. You can reference the object within the class in the same way. The real difference is in the way your overall application uses the database object; if another object within the script needs database access, then do

Code: Select all

// instantiate an independant database object
$dbObject= new DB();
// pass to the Foo object instance for use
$newFoo= new Foo($dbObject);
// another object requires database access
$newBar= new Bar($dbObject);
OTOH, if your object is the only one that requires database access, it's perfectly acceptable to instantiate a new database object within yours, as was shown in feyd's code example.