Jump to content

Recommended Posts

<?php

error_reporting(E_ALL);

class foo extends bar {

function foo() {
	$this->func_two();
	$this->my_func();
}

function my_func() {
	$this->home = new home;
	$this->home->index();
}
}

class bar {

function bar() {
	//__construct
}

function func_two() {
	$this->hello = new what;
}
}

class what {

function what() {
	//__construct
}

function what_test() {
	echo "hello";
}
}

class home extends foo {

function home() {
	//__construct
}

function index() {
	$this->hello->what_test();
}
}


$wow = new foo;


?>

 

I'm writing an MVC framework and to keep it simple i wrote the above code for example on what my framework is doing.

The problem is it dosen't seem that the class home is seeing or able to use the what class even though it's nested in a class extended by the foo class.

I've tried everything I can think of but it's just not working the way I want.

Any insight to this would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/144078-solved-nested-class-problem/
Share on other sites

It looks like your home constructor does not call the parent constructor, so $this->hello is not initialized.

 

Try this:

class home extends foo {
   
   public function __construct home() {
       parent::construct();
   }
   
   function index() {
      $this->hello->what_test();
   }
}

Another method for you to take into consideration is below if you wish to use another objects internals.

Your method of this extends this and extends this and extends this is not always good design and will lead to a headache.

 

Take this example:

 

class foo {
  public function x() {
	print "foo prints x";
  }
}

class bar {
	public function y(foo $foo) {
		$foo->x();
	}
}

$obj = new bar();
$obj->y(new foo());

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.