Class Scope

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Class Scope

Post by Benjamin »

How do I access and call functions in a database class that I wrote from within a login class that I wrote?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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();
  }
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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..

}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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().
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Figured that out right before you posted. Thanks a lot for the help.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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);

?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Could I have used

Code: Select all

$GLOBALS['Database']
rather than passing the $Database variable to the class.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Jenk's doesn't recreate the object, just stores the reference.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Ok I see.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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.
Post Reply