views:

567

answers:

3

Hello,

I was wondering if it is allowed to create a class inside another class.

It's actually the database class

or, do I have to create it outside and then pass it in threw the constructor?

but then I have created it without knowing if I would need it

example:

class some{

if(.....){
include SITE_ROOT . 'applicatie/' . 'db.class.php';
$db=new db

thanks, Richard

+1  A: 

Yes, it is possible.

Zed
thanks, that´s helpful
Richard
+1  A: 

You can't define a class in another class. You should include files with other classes outside of the class. In your case, that will give you two top-level classes db and some. Now in the constructor of some you can decide to create an instance of db. For example:

include SITE_ROOT . 'applicatie/' . 'db.class.php';

class some {

    public __construct() {
        if (...) {
            $this->db = new db;
        }
    }

}
Lukáš Lalinský
A: 

It's impossible to create a class in another class in PHP. Creating an object(an instance of a class) in another object is a different thing.

Marius Burz