I am fiddling a little with this code of mine.
I have the base class:
Code: Select all
abstract class UserDummy
{
function __construct($userID, $userEmail)
{
}
}Code: Select all
class User extends UserDummy
{
private $db;
private $userID;
function __construct(mysqlconnector $db, $userID)
{
$this->db = new mysqlconnector;
$this->userID = $userID;
}
function checkType()
{
return 'user';
}
function changeUserInformation($row, $information)
{
// Some checking
$this->db->execute_query("UPDATE users SET $row = ".$this->db->quoteSmart($information)." WHERE id = '$this->userID'");
}
function ()
{
echo "lol";
}
}Code: Select all
class Admin extends User
{
private $db;
private $userID;
private $userEmail;
function __construct(mysqlconnector $db, $userID, $userEmail)
{
$this->db = $db;
$this->userID = $userID;
$this->userName = $userEmail;
}
function checkType()
{
return 'admin';
}
function sendNewsletter($titel, $besked)
{
// Some code
}
}http://dk2.php.net/manual/en/language.oop5.basic.phpA class can inherit methods and members of another class by using the extends keyword in the declaration. It is not possible to extend multiple classes, a class can only inherit one base class.
The inherited methods and members can be overridden, unless the parent class has defined a method as final, by redeclaring them within the same name defined in the parent class. It is possible to access the overridden methods or members by referencing them with parent::
So it should be possible to do.
I have a function called login() that determines which classobject (user, admin) to put inside a session.
Calling
Code: Select all
echo $_SESSION[s_user_object]->checkType();However, the lol() function in User which Admin should have inherited doesn't work:
Code: Select all
echo $_SESSION[s_user_object]->lol();What on earth am i doing wrong?