Jump to content

class inside of a class??


ohdang888

Recommended Posts

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();

Link to comment
https://forums.phpfreaks.com/topic/118277-class-inside-of-a-class/
Share on other sites

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?

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.

Well, not yet anyway. Who knows what PHP7 holds :P

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.

Well, not yet anyway. Who knows what PHP7 holds :P

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.