Yesideez Posted February 20, 2009 Share Posted February 20, 2009 Is this possible? For example, class outer { function out() { class inner { function in() { return 'here!'; } } $doit=new inner(); return $this->doit->two(); } } $test=new outer(); echo $test->out(); I get no error when defining the second class but I can't call it. Quote Link to comment https://forums.phpfreaks.com/topic/146154-solved-nested-classes/ Share on other sites More sharing options...
Yesideez Posted February 20, 2009 Author Share Posted February 20, 2009 My bad - head in a spin and I got muddled. Been using classes all day at work in a pre-made environment and trying to learn them at home. Quote Link to comment https://forums.phpfreaks.com/topic/146154-solved-nested-classes/#findComment-767303 Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 I do not think it is possible. You can use class outer extends inner { As far as doing that, yea doubtful. You can however include a class from another file. class outer { function out() { include('class.inner.php'); $doit=new inner(); return $this->doit->two(); } } $test=new outer(); echo $test->out(); Should work. You could also just define that class on the same page and that should work as well. EDIT: I do not think you had the function "two" defined in the inner class (I think that is what you caught and solved it). Just decided to point it out EDITEDIT: Fatal error: Class declarations may not be nested in C:\wamp\www\test.php on line 4 Returned that when trying to nest Quote Link to comment https://forums.phpfreaks.com/topic/146154-solved-nested-classes/#findComment-767304 Share on other sites More sharing options...
rhodesa Posted February 20, 2009 Share Posted February 20, 2009 technically speaking, it's legal (i think), as long as you only try to declare the inner class once. the proper way to do what you want though is like this: <?php class outer { function out() { $this->doit=new inner(); return $this->doit->in(); } } class inner { function in() { return 'here!'; } } $test=new outer(); echo $test->out(); ?> edit: I take it back, nesting them produces a Fatal Error Quote Link to comment https://forums.phpfreaks.com/topic/146154-solved-nested-classes/#findComment-767305 Share on other sites More sharing options...
Maq Posted February 20, 2009 Share Posted February 20, 2009 There's no need to have a nested class. That won't even work. You're supposed to use extend, which is what it was created for. Or, if you need an interface, you use implements. Quote Link to comment https://forums.phpfreaks.com/topic/146154-solved-nested-classes/#findComment-767307 Share on other sites More sharing options...
Yesideez Posted February 20, 2009 Author Share Posted February 20, 2009 Thanks guys, making classes are very new to me and as I've recently started a new job using PHP I'm using them (as mentioned) in an environment already created - just modifying existing code. It's given me many headaches so far as seeing that my website has a "web1.0" look with all tables I'm re-designing the lot with classes and DIVs. I've managed to nest classes by including a PHP file inside a class - works fine. Quote Link to comment https://forums.phpfreaks.com/topic/146154-solved-nested-classes/#findComment-767309 Share on other sites More sharing options...
rhodesa Posted February 20, 2009 Share Posted February 20, 2009 I've managed to nest classes by including a PHP file inside a class - works fine. make sure you use require_once() when including, that way it doesn't get included more then once Quote Link to comment https://forums.phpfreaks.com/topic/146154-solved-nested-classes/#findComment-767317 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.