- Joined
- Jun 21, 2010
- Messages
- 325
- Reaction score
- 54
Hello,
I cannot for the life of me, get my head around using variables from a parent class and so on. I've searched everywhere and I seriously cannot get anywhere.
Anyhow, I have a Users & Database class. Database class handles database connection and users runs queries that are relevant to users, like adding, removing and updating things and such.
I wish to extend my database class to the user class so that I am able to use the connection variable with my queries. I can't think of a better way, I'm a noobie at this still. For an example, let's say I want to get the host_info for my current connection, when in an extended class.
My Database Class:
I'm not sure if I would redeclare the variable to get access to it, I'm not sure what to do at this point.
And this is where I would like to echo it.
I know it's connected properly, I know that for a fact, it's just I want to know how to access an extended class, pretty much my primary question.
Think anyone can help?
I cannot for the life of me, get my head around using variables from a parent class and so on. I've searched everywhere and I seriously cannot get anywhere.
Anyhow, I have a Users & Database class. Database class handles database connection and users runs queries that are relevant to users, like adding, removing and updating things and such.
I wish to extend my database class to the user class so that I am able to use the connection variable with my queries. I can't think of a better way, I'm a noobie at this still. For an example, let's say I want to get the host_info for my current connection, when in an extended class.
My Database Class:
PHP:
<?php
class Database
{
public $connection;
function Connect($hostname, $username, $password, $database)
{
return $this->connection = new mysqli($hostname, $username, $password, $database);
}
}
?>
PHP:
<?php
class Users extends Database
{
public $connection;
function Connection()
{
return $this->connection->host_info;
}
}
?>
I'm not sure if I would redeclare the variable to get access to it, I'm not sure what to do at this point.
PHP:
<?php
require_once("inc/classes/class.database.php");
require_once("inc/classes/class.users.php");
$database = new Database();
$users = new Users();
// Connect to database.
$database->Connect(HOSTNAME, USERNAME, PASSWORD, DATABASE);
echo $users->Connection();
?>
And this is where I would like to echo it.
I know it's connected properly, I know that for a fact, it's just I want to know how to access an extended class, pretty much my primary question.
Think anyone can help?

