Php 为什么在我将对象传递给方法时会创建它?
这是我的密码:Php 为什么在我将对象传递给方法时会创建它?,php,function,oop,object,Php,Function,Oop,Object,这是我的密码: public function call_with_attempts($class_obj, $method_name,array $params = [], $attempts = 5) { while ($attempts-- > 0) { try { if (call_user_func(array($class_obj, $method_name), $params)['result_code'] == '01') {
public function call_with_attempts($class_obj, $method_name,array $params = [], $attempts = 5) {
while ($attempts-- > 0) {
try {
if (call_user_func(array($class_obj, $method_name), $params)['result_code'] == '01') {
// log success
return $class_obj->$method_name();
}
throw new Exception("failed");
} catch (Exception $e) {
trigger_error($e->getMessage());
// log the error
}
}
}
我这样称呼它:
$url = 'www.example.com';
$obj = new Profile($url);
all_with_attempts($obj, 'mymethod');
下面是简介
课程:
class profile {
public $url;
public function __construct( $passed_url )
{
$this->url = $passed_url;
}
public function mymethod(){
// do stuff
}
}
任何我的代码(当我调用此函数时
all_并尝试($obj,'mymethod');
)。抛出此错误:
在{/path}中调用的App\classes\Profile::_construct()缺少参数1
如您所知,此错误^意味着将再次创建
Profile
类。。那不是我想要的。我想使用$obj
。我该怎么做呢?可能先检查传递的参数是否是对象:如果(是对象($class\u obj))
如果是,然后使用如果(方法存在($class\u obj,$method\u name))
然后用该方法返回该类,或者如果传递字符串,则使用调用用户函数()
?可能首先检查传递的参数是否是对象:如果(是对象($class\u obj))
,如果是,则使用如果(方法存在($class\u obj,$method\u name))
然后使用该方法返回该类,或者如果传递字符串,则使用调用用户函数()
?