Sasuun Posted May 3, 2008 Share Posted May 3, 2008 I'm having problems with my classes inheriting methods and functions. In the past I've used a different method of accessing all of my classes within each other by making pointers to a class wrapping around all of my other classes so they interact with each other nicely. I did something like this $Class = new Class; $Class->someclass = new someclass; $Class->someclass->class = &$class; This time, I thought I'd try using inheritance for my controllers, so I could access all of my classes by wrapping them inside of an App_Base class class someController extends App_Base { someAction() { $this->inheritedClass->inheritedMethod() } } but my class isn't inheriting properly, Instead it only gets the values of variables at initiation of the original class, so my $requestPath, for example which is populated after initialization remains the same. Am I doing something wrong, or is that just how inheritance works. I've included my dispatch function if it helps anyone, but I suspect the answer does not require reading it function dispatch( $routes = null ) { //////////////////////////////////////////////////// // do we have route rules present? //////////////////////////////////////////////////// define( 'USE_ROUTES', ( $routes != null) ? true:false ); $controller = strtolower( $this->getController( )); if ( $controller == "" ) { $controller = $this->config->defaultController; $action = strtolower( $this->getAction( 0 )); define('IS_DEFAULT_CONTROLLER', true); } else { $action = strtolower( $this->getAction( )); define('IS_DEFAULT_CONTROLLER', false); } if ( $action == "" ) { $action = 'index'; } if ( USE_ROUTES ) { //////////////////////////////////////////////////// // Routes... check to see if the specified controller has a route... //////////////////////////////////////////////////// if ( array_key_exists( $controller, $routes )) { $action = $routes[ $controller ][ 'action' ]; $controller = $routes[ $controller ][ 'controller' ]; #reset our information to fit the route, set the action first or our controller will be overwriten with the new one } } //////////////////////////////////////////////////// // check to see if the controller is present in the CONTROLLER_ROOT //////////////////////////////////////////////////// if ( file_exists( CONTROLLER_ROOT . $controller . "Controller.php" )) { require_once CONTROLLER_ROOT . $controller . "Controller.php"; $controllerName = $controller . "Controller"; $action = $action . "Action"; if ( class_exists( $controllerName )) { $Controller = new $controllerName; //check for everything and execute, if there is a problem, we throw an exception if ( method_exists( $Controller, $action )) { $Controller->$action( ); } else { if ( method_exists( $Controller, 'indexAction' )) { $Controller->indexAction(); } else { $this->throwError( 'No index action defined' ); } } } else { $this->throwError( $controllerName . " does not exist in " . $controllerName . ".php" ); } } else { $this->throwError( $controller . "Controller does not exist" ); } } Quote Link to comment https://forums.phpfreaks.com/topic/103953-inheritance-problem/ Share on other sites More sharing options...
aschk Posted May 6, 2008 Share Posted May 6, 2008 Just looking at your first code post: $Class = new Class; $Class->someclass = new someclass; $Class->someclass->class = &$class; If you're using PHP5 you don't need to specify the & (reference) symbol. And instead I recommend using the "strategy" pattern. I forsee the code being a bit more like this: $class = new Class(); $class->setOtherInternalClass( new someClass() ); And internally you have a structure like this: class Class { private $someClass; public function setOtherInternalClass( someClass $someClass) { $this->someClass = $someClass; $this->someClass->setInitializer( $this ); } } class someClass { private $initializer; public function setInitializer( Class $class ) { $this->$intializer = $class; } } Bear in mind the above is VERY tightly coupled. In regard to your 2nd code listing. If you want to access an inherited method (from a parent) then you just need to call that method. e.g. class App_Base { public function baseMethod(){ echo "this is a method from the base (parent) class"; } } class someController extends App_Base { public function newChildMethod(){ $this->baseMethod(); // because our class inherits from the App_Base class it has access to all its methods. } } I think you're confusing what inheritance is. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/103953-inheritance-problem/#findComment-534231 Share on other sites More sharing options...
448191 Posted May 6, 2008 Share Posted May 6, 2008 You need some basic OOP guidance. I feel dirty just reading this $Class = new Class; Quote Link to comment https://forums.phpfreaks.com/topic/103953-inheritance-problem/#findComment-534252 Share on other sites More sharing options...
deadimp Posted May 9, 2008 Share Posted May 9, 2008 http://www.php.net/manual/en/language.oop5.php It looks like you're trying to find a way to call a parent's method. If it isn't defined in the child class, you can simply call it as $this->blarg(). If blarg() is overloaded (right for polymorphism terminology?) in the child class, you can explicitly call the parent's method using parent::blarg() inside child::blarg() - though you can't explicitly call the parent's method from a child object outside an object's method scope. If you're sure of your design, and you want to use that explicit calling stuff, here's an example: class Base { //Example with ctor public function __construct() { echo "Base::ctor\n"; } public function bleck() { echo "Base::bleck\n"; } public function test() { echo "Base::test\n"; } } class Child extends Base { public function __construct() { parent::__construct(); //Explicitly call parent's ctor method //Do your own stuff here echo "Child::ctor\n"; } public function bleck() { $this->test(); echo "Blah"; } } $obj=new Child(); $obj->bleck(); /*Output: Base::ctor Child::ctor Base::test Blah */ Just so you know, every class inherits stdClass. Unless making your own base object for your system is a dire need, use what PHP already has. Also, what's the reason behind manually setting a reference in an object to itself? That's the exact reason for the the $this keyword. Quote Link to comment https://forums.phpfreaks.com/topic/103953-inheritance-problem/#findComment-536477 Share on other sites More sharing options...
aschk Posted May 13, 2008 Share Posted May 13, 2008 I've seen a class setting a reference to itself before, in C++... don't ask me why though. Quote Link to comment https://forums.phpfreaks.com/topic/103953-inheritance-problem/#findComment-539730 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.