trix21 Posted February 6, 2009 Share Posted February 6, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/144078-solved-nested-class-problem/ Share on other sites More sharing options...
JonnoTheDev Posted February 6, 2009 Share Posted February 6, 2009 Your what class is not extending anything. Look at your code: class what { Quote Link to comment https://forums.phpfreaks.com/topic/144078-solved-nested-class-problem/#findComment-756098 Share on other sites More sharing options...
trix21 Posted February 6, 2009 Author Share Posted February 6, 2009 I've tried extending it on foo but I still get the same problem, which is: Fatal error: Call to a member function on a non-object in **** on line 47 Quote Link to comment https://forums.phpfreaks.com/topic/144078-solved-nested-class-problem/#findComment-756102 Share on other sites More sharing options...
flyhoney Posted February 6, 2009 Share Posted February 6, 2009 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(); } } Quote Link to comment https://forums.phpfreaks.com/topic/144078-solved-nested-class-problem/#findComment-756105 Share on other sites More sharing options...
JonnoTheDev Posted February 6, 2009 Share Posted February 6, 2009 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()); Quote Link to comment https://forums.phpfreaks.com/topic/144078-solved-nested-class-problem/#findComment-756113 Share on other sites More sharing options...
trix21 Posted February 6, 2009 Author Share Posted February 6, 2009 maybe I should mention that I want this to be backwards compatible with php 4. So unfortunately even though i know that using public functionality would work it won't work in php 4. Yes this has given me a big headache lol. Quote Link to comment https://forums.phpfreaks.com/topic/144078-solved-nested-class-problem/#findComment-756118 Share on other sites More sharing options...
flyhoney Posted February 6, 2009 Share Posted February 6, 2009 class home extends foo { function home() { parent::foo(); } function index() { $this->hello->what_test(); } } Quote Link to comment https://forums.phpfreaks.com/topic/144078-solved-nested-class-problem/#findComment-756122 Share on other sites More sharing options...
trix21 Posted February 6, 2009 Author Share Posted February 6, 2009 I just tried that right when you posted that last one... crazy enough i'm getting a 500 error. Quote Link to comment https://forums.phpfreaks.com/topic/144078-solved-nested-class-problem/#findComment-756124 Share on other sites More sharing options...
trix21 Posted February 6, 2009 Author Share Posted February 6, 2009 Ok I figured it out, I was initializing home incorrectly, but that did work thanks alot. Quote Link to comment https://forums.phpfreaks.com/topic/144078-solved-nested-class-problem/#findComment-756135 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.