getinstance($object); $obj->$function($client_id, $res); } catch (\Exception $e) { Tools::error_txt_log($e); $json = BaseWssApi::json_wss('error', '服务繁忙'); Gateway::sendToClient($client_id, json_encode($json, JSON_UNESCAPED_UNICODE)); } } function getinstance($className) { // 类名字符串 // 参数数组 // 确保类存在 if (!class_exists($className)) { throw new \InvalidArgumentException("Class {$className} does not exist."); } // 创建反射类实例 $reflection = new \ReflectionClass($className); // 检查构造函数是否存在 //if (!$reflection->hasMethod('__construct')) { // throw new \LogicException("Class {$className} has no constructor."); //} //$constructor = $reflection->getConstructor(); // 如果构造函数有参数,我们需要匹配参数 //if ($constructor !== null) { // $constructorParams = $constructor->getParameters(); // // // 确保参数数量匹配 // if (count($constructorParams) !== count($params)) { // throw new \InvalidArgumentException("Number of constructor parameters does not match provided arguments."); // // } // // 创建参数数组,将参数类型与值匹配 // $matchedParams = []; // foreach ($constructorParams as $index => $param) { // // 如果参数允许null,或者参数类型与传递的值兼容,添加到匹配参数数组 // if ($param->allowsNull() || $param->getClass() === null || $param->getClass()->isInstance($params[$index])) { // $matchedParams[] = $params[$index]; // } else { // throw new \InvalidArgumentException("Provided argument does not match constructor parameter type at position {$index}."); // } // } // // 使用反射类创建并初始化类实例 // $instance = $reflection->newInstanceArgs($matchedParams); //} else { // //} // 构造函数无参数,直接创建实例 $instance = $reflection->newInstanceWithoutConstructor(); return $instance; } }