我有这个问题:
<?php
class A {
}
class B {
}
$objectsInArray = array();
$objectsInArray[] = new A();
$objectsInArray[] = new B();
class C {
private $a;
private $b;
public function __construct(A $a, B $b) {
$this->a = $a;
$this->b = $b;
}
}
如何像这样直接将$objectInArray传递给 C() 类:
$c = new C($objectsInArray);
没有此错误消息:
Catchable fatal error: Argument 1 passed to C::__construct() must be an instance of A, array given...
我不想要这个理由:
class C {
private $a;
private $b;
public function __construct(array $arguments) {
foreach ($arguments as $argument) {
if ($argument instanceof A) {
$this->a = $argument;
} elseif ($argument instanceof B) {
$this->b = $argument;
} else {
throw new exception('Arguments are bad!');
}
}
}
}
感谢您的回答。