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
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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.