Reputation: 927
At the moment, I have a main class Base
that loads all other controllers and models and then each class that is loaded by Base
has a similar structure:
class SomeClass {
private $base;
function __construct(&$base) {
$this->base = $base;
}
function SomeMethod() { }
}
Then another class would have to use:
class AnotherClass {
private $base;
function __construct(&$base) {
$this->base = $base;
$this->base->SomeClass->SomeMethod();
}
}
Is there a better way of accessing these other classes?
Upvotes: 1
Views: 480
Reputation: 1659
Perhaps someMethod() could be static:
class SomeClass {
private $base;
function __construct(&$base) {
$this->base = $base;
}
public static function SomeMethod() { }
}
And then just:
class AnotherClass {
private $base;
function __construct(&$base) {
$this->base = $base;
SomeClass::SomeMethod();
}
}
Upvotes: 1
Reputation: 42547
It sounds like Base
is an implementation of the Front Controller pattern. The Front Controller is a special case of a Mediator, which does exactly what you're doing. It essentially allows SomeClass
and AnotherClass
to be developed and maintained separately with fewer dependencies.
However, rather than directly accessing the classes from the Base
class, it might be best to have SomeClass
and AnotherClass
register themselves with the Base
class, and expose getter methods that the other objects call:
class Base {
protected $_authenticator;
public function setAuthenticator(Authenticator $auth) {
$this->_authenticator = $auth;
}
public function getAuthenticator() {
return $this->_authenticator;
}
}
class Authenticator {
protected $_base;
public function __construct(Base $base) {
$this->_base = $base;
$this->_base->setAuthenticator($this);
}
}
Upvotes: 1