Everything looks mostly fine, but you should move the variable set calls into the constructor for class DBConnection (it doesn't look they're in there no, but that may just be the code snippet).
Please note, in the current implemenation of OO in PHP, classes do not automatically call their parent's constructor. Thus, you need to place a call to the DBConnection constructor from within the User constructor. Clear as mud? Hopefully the following modification to your example will illustrate my point🙂
N.B. Note that I'm creating the User object by reference..... you should always do this unless you're creating and object you intend to pass aroud as a global.
<?php
//DBCOnnection object
class DBConnection {
var $hostname;
var $username;
var $password;
var $dbName;
var $numRows;
function DBConnection($hostname="",$username="",$password="",$dbName="")
{
// set the connection variables
$hostname == "" ? $this->hostname = "localhost" : $this->hostname = $hostname;
$username == "" ? $this->username = "origin" : $this->username = $username;
$password == "" ? $this->password = "CNuyMNhX" : $this->password = $password;
$dbName == "" ? $this->dbName = "originxcreative_com" : $this->dbName = $dbName;
$this->numRows = 0;
}
function openConnection()
{
//Opens a connection to the MySQL database
MYSQL_CONNECT($this->hostname, $this->username, $this->password) or die("Unable to connect to database. Reason: " . mysql_error());
@MYSQL_SELECT_DB($this->dbName) or die( "Unable to select database. Reason: " . mysql_error());
}
}
//User object
class User extends DBConnection {
function User() {
$this->DbConnection();
}
}
$UserBean = &new User();
$UserBean->openConnection();
?>
HTH.
-geoff