ohdang888 Posted August 5, 2008 Share Posted August 5, 2008 how do i make a class inside of another class?? i'm getting this error: Parse error: syntax error, unexpected T_CLASS, expecting T_FUNCTION in C:\xampp\htdocs\lib.php on line 8 <?php class gg{ class profile{ function bla(){ echo "Hello"; } } }//end gg class $go = new gg(); $profile = $go->new profile(); $profile->bla(); Quote Link to comment https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/ Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 Don't do it like that. Just do: class gg { public function createProfile() { return new profile(); } } class profile { public function blah() { echo "Hello."; } } $profile = new gg->createProfile(); Though that's not the best way, unless you have a reason for instantiating inside of another class (i.e: there's some logic you need to take care of in order to instantiate properly). Can you provide some real code and maybe we can think of a better way? Quote Link to comment https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/#findComment-608658 Share on other sites More sharing options...
KevinM1 Posted August 5, 2008 Share Posted August 5, 2008 how do i make a class inside of another class?? i'm getting this error: Parse error: syntax error, unexpected T_CLASS, expecting T_FUNCTION in C:\xampp\htdocs\lib.php on line 8 <?php class gg{ class profile{ function bla(){ echo "Hello"; } } }//end gg class $go = new gg(); $profile = $go->new profile(); $profile->bla(); I don't believe you can nest classes in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/#findComment-608659 Share on other sites More sharing options...
LemonInflux Posted August 5, 2008 Share Posted August 5, 2008 Well, not yet anyway. Who knows what PHP7 holds Quote Link to comment https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/#findComment-608663 Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 Lol, PHP6 is going to cause enough problems for everyone. =P (Bye bye, register_globals) Quote Link to comment https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/#findComment-608665 Share on other sites More sharing options...
LemonInflux Posted August 5, 2008 Share Posted August 5, 2008 People actually still rely on register_globals? Quote Link to comment https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/#findComment-608673 Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 You'd be surprised by the amount of posts here that rely on register globals. >_< Quote Link to comment https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/#findComment-608678 Share on other sites More sharing options...
obsidian Posted August 5, 2008 Share Posted August 5, 2008 Well, not yet anyway. Who knows what PHP7 holds If they tried to support defining classes inside of other classes, I would probably choke. That seems like it would go directly against OOP principles. You'd be surprised by the amount of posts here that rely on register globals. >_< Much of the dependency on register_globals is due to a barrage of older open source software still in religious use. Many old forums and other software that people no longer support is still in use, and rather than upgrading or changing to a new system, some people tend to want to modify the old one for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/#findComment-608693 Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 Well, not yet anyway. Who knows what PHP7 holds If they tried to support defining classes inside of other classes, I would probably choke. That seems like it would go directly against OOP principles. You'd be surprised by the amount of posts here that rely on register globals. >_< Much of the dependency on register_globals is due to a barrage of older open source software still in religious use. Many old forums and other software that people no longer support is still in use, and rather than upgrading or changing to a new system, some people tend to want to modify the old one for some reason. Yeah, I know. Many posts are like: "Well I downloaded this script 7 years ago and just decided to install it now and it doesn't work." >_> And yeah, they'd never allow classes inside of classes because 1) it has no point whatsoever, and 2) yeah, it goes against OOP a bit. Quote Link to comment https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/#findComment-608697 Share on other sites More sharing options...
ignace Posted August 7, 2008 Share Posted August 7, 2008 only java currently supports to declare a class inside of a class, if you would like to do this in php you should look at class composition or class aggregation Quote Link to comment https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/#findComment-610505 Share on other sites More sharing options...
jordanwb Posted August 11, 2008 Share Posted August 11, 2008 C# does as well. Quote Link to comment https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/#findComment-613934 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.