I am starting to get into OOP with PHP 5. Can someone please tell me why this doesn't work:

<?php
class Base {
   public $test = NULL;

   public function __construct() {
	   $this->load();
   }

   public function load() {
		$this->test = new Test();

   }
}

class Test {
   public $var1 = 'test1';
   public $var2 = NULL;

   public function __construct() {
		$this->var2 = 'test2';
   }
}

$base = new Base();

class Content extends Base {
   public function __construct() {
   	$this->main();
   }

  public function main() {
   		echo 'test<br>';
		echo $this->test->var1;
		echo '<br>';
		echo $this->test->var2;
   }
}

$content = new Content();
?>

It only outputs the first echo of "test". What I want to do is create a base class that starts all the other classes and then have a class that extends base that can call everything in base and the other classes.

In my example Base starts Test which sets some variables. Then I create Content which extends base. I want to be able to call var1 and var2 in the Test class.

I might be all messed up. Any help would be greatly appreciated.

Thanks,
Alex

    The parent constructor isn't being called. Changes shown below.

    class Content extends Base {
       public function __construct() {
           parent::__constructor();
           $this->main();
       }
    

      Thanks for the reply, but that gives me an error

      Fatal error: Call to undefined method Base::__constructor()

        Nevermind I got it. Should be parent::construct(); instead of parent::constructor();

        Thanks for pointing that out to me!

          wow, something funny happened there and i posted twice.

          If the problem is fixed now, don't forget to mark the thread resolved under thread tools

            Write a Reply...