Class Scope
Moderator: General Moderators
Class Scope
How do I access and call functions in a database class that I wrote from within a login class that I wrote?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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();
}
}The database class is instantiated before the login class is though...
header.php
classes.php
header.php
Code: Select all
$Database = new databaseConnection();
$Database->connect($host, $username, $password);
$Database->selectDatabase($database);
$Login = new login;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..
}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);
?>Could I have used rather than passing the $Database variable to the class.
Code: Select all
$GLOBALS['Database']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
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.
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);