所以我是面向对象编程世界的新手,我目前面临这个问题(所有内容都在代码中描述):
<?php
class MyClass {
// Nothing important here
}
class MyAnotherClass {
protected $className;
public function __construct($className){
$this->className = $className;
}
public function problematicFunction({$this->className} $object){
// So, here I obligatorily want an $object of
// dynamic type/class "$this->className"
// but it don't works like this...
}
}
$object = new MyClass;
$another_object = new MyAnotherClass('MyClass');
$another_object->problematicFunction($object);
?>
谁能帮我吗?
谢谢,马克西姆(来自法国:对不起我的英语)
最佳答案
你需要的是
public function problematicFunction($object) {
if ($object instanceof $this->className) {
// Do your stuff
} else {
throw new InvalidArgumentException("YOur error Message");
}
}
关于PHP : Function argument must be an Object with dynamic class name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15065241/