Can I instantiate a PHP class inside another class?
I was wondering if it is allowed to c开发者_如何转开发reate an instance of a class inside another class.
Or, do I have to create it outside and then pass it in through the constructor? But then I would have created it without knowing if I would need it.
Example (a database class):
class some{
if(.....){
include SITE_ROOT . 'applicatie/' . 'db.class.php';
$db=new db
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.
I just wanted to point out that it is possible to load a class definition dynamically inside another class definition.
Lukas is right that we cannot define a class inside another class, but we can include() or require() them dynamically, since every functions and classes defined in the included file will have a global scope. If you need to load a class or function dynamically, simply include the files in one of the class' functions. You can do this:
function some()
{
require('db.php');
$db = new Db();
...
}
http://php.net/manual/en/function.include.php
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.
No, it is not possible. Nesting classes and aggregating objects are diffrent mechanisms. Nesting classes means nesting class names, in fact. Class A nested in class B would be named "B\A" (?). From v5.3 you can nest class names (and some other names) in namespaces.
This is possible in PHP 7 with Anonymous classes.
See the example from the docs:
class Outer
{
private $prop = 1;
protected $prop2 = 2;
protected function func1()
{
return 3;
}
public function func2()
{
return new class($this->prop) extends Outer {
private $prop3;
public function __construct($prop)
{
$this->prop3 = $prop;
}
public function func3()
{
return $this->prop2 + $this->prop3 + $this->func1();
}
};
}
}
echo (new Outer)->func2()->func3();
// Output: 6
This is a simple example showing usage of anonymous classes, but not from within a class:
// Pre PHP 7 code
class Logger
{
public function log($msg)
{
echo $msg;
}
}
$util->setLogger(new Logger());
// PHP 7+ code
$util->setLogger(new class {
public function log($msg)
{
echo $msg;
}
});
(Edit) Ok the question and the example of what you are trying to accomplish are confusing. It seems from your example you are trying to DEFINE a class inside another class.
The correct name for this is NESTED CLASS and it is not possible in PHP. So, to your example the answer is "NO". Not in PHP anyway. Other languages will allow "nested classes". Java is an example.
For example this will NOT work in PHP
class outer_class{
class inner_class{
...
}
...
}
Now the question asked is to create AN INSTANCE of a class in another class.
To this the answer is "YES", you can INSTANTIATE an "object" inside a "class". We do it all the time.
class simple_class{
protected $instanceOfOtherClass;
public function instanciateObject_KeepingAReferenceToIt(){
// create an instance of OtherClass and associate it to $this->instanceOfOtherClass
$this->instanceOfOtherClass = new OtherClass();
}
public function instanciateObject_Without_KeepingAReferenceToIt(){
// create an instance of OtherClass and return it
return new OtherClass();
}
}
A class can even instantiate itself. That's what the famous singleton does and many other Creational patterns. Sadly some people believe that knowing about dependency injection means they will never call NEW in a method again. WRONG.
It helps to understand the difference between a class and an object. A class is an extensible program-code-template for creating objects. An object refers to a particular instance of a class.
I'm just seeing this now and I wish to add my comment. It could be of benefit to anyone. Has anyone heard of php traits? Although traits does not import another class but only imports a trait into another class. So instead of trying to import several classes, you can have a main classe while the subclasses will act like traits making it possible for you to use them in main/parent classes
精彩评论