Jump to content

Inheritance Problem


Sasuun

Recommended Posts

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" );
	}


}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.