/** * Set the names of the mandatory and optional arguments to the method * */ public function setArgumentNames() { $methodClass = new reflectionMethod($this->className, $this->methodName); $parameters = $methodClass->getParameters(); foreach ($methodClass->getParameters() as $i => $param) { if ($param->isOptional()) { $this->optionalArgumentNames[] = $param->getName(); } else { $this->mandatoryArgumentNames[] = $param->getName(); } } }
/** * Returns an array with parameter objects, containing type info etc. * * @return ReflectionParameter[] Associative array with parameter objects */ public function getParameters() { $this->parameters = array(); $ar = parent::getParameters(); $i = 0; foreach ((array) $ar as $parameter) { $parameter->type = $this->params[$i++]->type; $this->parameters[$parameter->name] = $parameter; } return $this->parameters; }
protected function getParametersSignature(\reflectionMethod $method) { $parameters = array(); $mustBeNull = $this->isOrphanized($method->getName()); foreach ($method->getParameters() as $parameter) { $parameterCode = self::getParameterType($parameter) . ($parameter->isPassedByReference() == false ? '' : '& ') . '$' . $parameter->getName(); switch (true) { case $parameter->isDefaultValueAvailable(): $parameterCode .= ' = ' . var_export($parameter->getDefaultValue(), true); break; case $parameter->isOptional(): case $mustBeNull: $parameterCode .= ' = null'; } $parameters[] = $parameterCode; } if ($method->isConstructor() === true) { $parameters[] = '\\' . __NAMESPACE__ . '\\controller $mockController = null'; } return join(', ', $parameters); }
/** * Load a module. * * 1. include the control file or the extension action file. * 2. create the control object. * 3. set the params passed in through url. * 4. call the method by call_user_function_array * * @access public * @return bool|object if the module object of die. */ public function loadModule() { $moduleName = $this->moduleName; $methodName = $this->methodName; /* Include the contror file of the module. */ $file2Included = $this->setActionExtFile() ? $this->extActionFile : $this->controlFile; chdir(dirname($file2Included)); include $file2Included; /* Set the class name of the control. */ $className = class_exists("my{$moduleName}") ? "my{$moduleName}" : $moduleName; if (!class_exists($className)) { $this->triggerError("the control {$className} not found", __FILE__, __LINE__, $exit = true); } /* Create a instance of the control. */ $module = new $className(); if (!method_exists($module, $methodName)) { $this->triggerError("the module {$moduleName} has no {$methodName} method", __FILE__, __LINE__, $exit = true); } $this->control = $module; /* include default value for module*/ $defaultValueFiles = glob($this->getTmpRoot() . "defaultvalue/*.php"); if ($defaultValueFiles) { foreach ($defaultValueFiles as $file) { include $file; } } /* Get the default setings of the method to be called useing the reflecting. */ $defaultParams = array(); $methodReflect = new reflectionMethod($className, $methodName); foreach ($methodReflect->getParameters() as $param) { $name = $param->getName(); $default = '_NOT_SET'; if (isset($paramDefaultValue[$className][$methodName][$name])) { $default = $paramDefaultValue[$className][$methodName][$name]; } elseif ($param->isDefaultValueAvailable()) { $default = $param->getDefaultValue(); } $defaultParams[$name] = $default; } /* Set params according PATH_INFO or GET. */ if ($this->config->requestType == 'PATH_INFO') { $this->setParamsByPathInfo($defaultParams); } elseif ($this->config->requestType == 'GET') { $this->setParamsByGET($defaultParams); } /* Call the method. */ call_user_func_array(array(&$module, $methodName), $this->params); return $module; }
<?php function __autoload($classname) { echo "__autoload({$classname})\n"; eval("class {$classname} {}"); } class B { public function doit(A $a) { } } $ref = new reflectionMethod('B', 'doit'); $parameters = $ref->getParameters(); foreach ($parameters as $parameter) { $class = $parameter->getClass(); echo $class->name . "\n"; } echo "ok\n";
protected static function hasVariadic(\reflectionMethod $method) { $parameters = $method->getParameters(); if (sizeof($parameters) === 0) { return false; } return self::isVariadic(end($parameters)); }
protected static function getParameters(\reflectionMethod $method, $addMockController = false) { $parameters = array(); foreach ($method->getParameters() as $parameter) { $parameterCode = self::getParameterType($parameter) . ($parameter->isPassedByReference() == false ? '' : '& ') . '$' . $parameter->getName(); if ($parameter->isDefaultValueAvailable() == true) { $parameterCode .= '=' . var_export($parameter->getDefaultValue(), true); } else { if ($parameter->isOptional() === true) { $parameterCode .= '=null'; } } $parameters[] = $parameterCode; } if ($addMockController === true) { $parameters[] = '\\' . __NAMESPACE__ . '\\controller $mockController = null'; } return join(', ', $parameters); }
/** * 加载一个模块: * 1. 引入控制器文件或扩展的方法文件; * 2. 创建control对象; * 3. 解析url,得到请求的参数; * 4. 使用call_user_function_array调用相应的方法。 * * Load a module. * 1. include the control file or the extension action file. * 2. create the control object. * 3. set the params passed in through url. * 4. call the method by call_user_function_array * * @access public * @return bool|object if the module object of die. */ public function loadModule() { $moduleName = $this->moduleName; $methodName = $this->methodName; /* * 引入该模块的control文件。 * Include the control file of the module. **/ $file2Included = $this->setActionExtFile() ? $this->extActionFile : $this->controlFile; chdir(dirname($file2Included)); include $file2Included; /* * 设置control的类名。 * Set the class name of the control. **/ $className = class_exists("my{$moduleName}") ? "my{$moduleName}" : $moduleName; if (!class_exists($className)) { $this->triggerError("the control {$className} not found", __FILE__, __LINE__, $exit = true); } /* * 创建control类的实例。 * Create a instance of the control. **/ $module = new $className(); if (!method_exists($module, $methodName)) { $this->triggerError("the module {$moduleName} has no {$methodName} method", __FILE__, __LINE__, $exit = true); } $this->control = $module; /* * 使用反射机制获取函数参数的默认值。 * Get the default settings of the method to be called using the reflecting. * * */ $defaultParams = array(); $methodReflect = new reflectionMethod($className, $methodName); foreach ($methodReflect->getParameters() as $param) { $name = $param->getName(); $default = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : '_NOT_SET'; $defaultParams[$name] = $default; } /** * 根据PATH_INFO或者GET方式设置请求的参数。 * Set params according PATH_INFO or GET. */ if ($this->config->requestType == 'PATH_INFO') { $this->setParamsByPathInfo($defaultParams); } elseif ($this->config->requestType == 'GET') { $this->setParamsByGET($defaultParams); } /* 调用该方法 Call the method. */ call_user_func_array(array(&$module, $methodName), $this->params); return $module; }
public function just_do_it() { # 通过反射机制设置函数的参数 $d_params = array(); $method_reflection = new reflectionMethod($this->cur_mname, $this->cur_func); foreach ($method_reflection->getParameters() as $param) { $param_name = $param->getName(); if (isset($this->cur_params[$param_name])) { $tmp = $this->cur_params[$param_name]; } else { if ($param->isDefaultValueAvailable()) { $tmp = $param->getDefaultValue(); } else { throw new Exception("参数'" . $param_name . "'不能为空. file:" . __FILE__ . " line:" . __LINE__); } } $d_params[$param_name] = $tmp; } $this->cur_params = $d_params; # 加载 call_user_func_array(array(&$this->modules[$this->cur_mname], $this->cur_func), $this->cur_params); }