I have 3 classes with the following inheritance structure:
<?php
class admin {
function __construct($module){
echo $module;
}
}
class user_admin extends admin {
function __construct(){
parent::__construct('user');
}
}
class sales_admin extends user_admin {
function __construct(){
parent::__construct('sales');
}
}
You'll notice that the sales_admin extends the user_admin, this is a nescessary step. When I run this code,
$a = new sales_admin;
it will echo "user", because it passes the "sales" string to the user_admin which doesn't accept a constructor.
Is there a way to access the constructor of the parent above it without changing the user_admin, which I don't have control over?
Just reference the class directly:
class sales_admin extends user_admin
{
function __construct()
{
admin::__construct('sales');
}
}
$a = new sales_admin; // outputs 'sales'
Since user_admin extends admin, and sales_admin extends user_admin, sales_admin will have scope of the admin constructor
//super implimentation php
class object extends other_object {
function __construct(){
$this->super();
}
function super() {
$parentClass = get_parent_class($this);
$this->$parentClass();
}
}
sales_admin knows it has a parent because it is extending user_admin. However, how can sales_admin possibly know if user_admin has a parent? In PHP, sales_admin does not have a way of knowing -- unless you cheat with reflection.
An option that you do have is to borrow admin's constructor. You can do this by calling admin::__construct('sales');
. Note that you can borrow the method of any class this way, regardless of the inheritance tree -- that is, the caller does not have to be a descendant of the callee.
Despite this option, I would recommend changing your design in such a way that you can avoid the inheritance altogether.