views:

59

answers:

2

Wondering if this is even possible or a limitation of PHP, googling around seems to be the case but maybe I'm missing a clever solution here.

Hopefully this will make sense. Right now I have two portions to my site, an admin and client side. I have been able to split it into two controllers(admin and client) that inherit from a base MY_Controller class, but the problem I have is implementing my authentication controller.

There is a lot of shared code between the client/admin logins, and right now I'd either have to create an auth controller for each admin and client that extends the base (which means a lot repeated code), or just extend the MY_Controller class and use if/thens in each function, which then becomes kind of messy (and also bypasses the admin/client parent classes which defeats the purpose of having them...).

I'm wondering if there is a way to have the Auth controller be "neutral"(?) and able to inherit from either the admin/client controllers, so if X is the base controller, and Y is the Auth...

X -> Admin Controller -> Y
X -> Client Controller -> Y

I am using an authentication library to encapsulate some of the lower level functions and would rather not be sticking layout logic and stuff like that in there. Forgive me if this is a stupid question. Thanks!

A: 

Multiple inheritance is not possible with PHP. I'm wondering though, why would you need two separate login controllers? Could you explain what you're doing in the controllers?

EDIT:
Not sure if your code allows this, but you could try putting all general parts in the client controller and let the admin controller extend from this one.

X -> Admin Ctrlr -> Client Ctrlr -> Y
X -> Client Ctrlr -> Y
Anzeo
Well I have separate logic for login/register depending on whether the user is on the admin or client portion of the website. For instance, if the user is registering on the admin portion I process a few more form fields, or ask for an invite code, etc. There are also portions of the login that are the same, like forgetting password, reset, etc.
Jeff
+1  A: 

Multiple inheritance is not possible. You either can use interfaces or you can use the visitor designpattern like this:

<?php

class A {
    public $avar = 'I\'m A';
    function accept(Visitor $v){
        $v->visitA($this);
    }
}

class B {
    public $bvar = 'B reporting';
    function accept(Visitor $v){
        $v->visitB($this);
    }
}


class Visitor {
    function visitA(A $a){
        echo $a->avar;
    }

    function visitB(B $b){
        echo $b->bvar;
    }
}


$A = new A();
$B = new B();
$visitor = new Visitor();

$A->accept($visitor);
$B->accept($visitor);

?>

unfortunately php is not ready yet for distinguishing method calls by their parameter like in java where this example would look like that:

class A {
    public String avar = 'I\'m A';
    function accept(Visitor v){
        v.visit(this);
    }
}

class B {
    public String bvar = 'B reporting';
    function accept(Visitor v){
        v.visit(this);
    }
}


class Visitor {
    function visit(A a){
        System.out.println(a.avar);
    }

    function visit(B b){
        System.out.println(b.bvar);
    }
}


A = new A();
B = new B();
visitor = new Visitor();

A.accept(visitor);
B.accept(visitor);

where you have multiple visit methods distinguished by the type of their parameters

ITroubs
so here you can do something like Admincontroller->accept($visitor) and in the visitor you have two methods one is named visitAdmin and the other is named visitUser AND when you don't know the type of the controller that has to be visited it is no problem because you give the controller the visitor and he decides which visitor method to call
ITroubs
You also should implement a little visitor interface with only the accept method and let the two controller implement that interface so that you can use typehinting in your methods where you give your controller to so that you can be shure they have that accept method
ITroubs
Attempting to play around with this method!
Jeff