gpt4 book ai didi

PHP : Function argument must be an Object with dynamic class name

转载 作者:行者123 更新时间:2023-12-04 05:09:27 26 4
gpt4 key购买 nike

所以我是面向对象编程领域的新手,我目前正面临这个问题(所有内容都在代码中进行了描述):

<?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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com