Codeigniter and Multiple Inheritance?
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!
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
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
精彩评论