Can I instantiate a PHP class inside another class?
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 function __construct() {
if (...) {
$this->db = new db;
}
}
}
People saying that it is possible to 'create a class within a class' seem to mean that it is possible to create an object / instance within a class. I have not seen an example of an actual class definition within another class definition, which would look like:
class myClass{
class myNestedClass{
}
}
/* THIS IS NOT ALLOWED AND SO IT WON'T WORK */
Since the question was 'is it possible to create a class inside another class', I can now only assume that it is NOT possible.