- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有下面的 PHP 遗留代码,旨在动态返回方法的名称。
public function getMethod($fieldName){
//do stuff
return $methodName;
}
返回的方法名称类似于:
setClient
setName
setAge
就在这里。问题是当我使用这些方法时。我有一个名为 Model 的类,它具有以下方法:
class Model {
public function find () {
$nameClass = get_called_class();
$instance = new $nameClass;
$result = $this->conn->select();//the select method returns a SQL SELECT from the database
if (isset($result[0])) {
foreach ($result[0] as $key => $val) {
$methodProp = $this->getMethod(key);
$instance->$methodProp($val);
}
} else {
throw new Exception('You have a error in your class');
}
}
}
这是请求的var_dump($result)
:
array (size=1)
0 =>
object(stdClass)[6]
public 'id' => string '1' (length=1)
public 'id_erp' => string '0' (length=1)
public 'name' => string 'Derp' (length=18)
public 'email' => null
public 'type_ota' => null
public 'user' => string 'derp_derp' (length=7)
public 'password' => string '1234' (length=4)
public 'url_logo' => null
public 'status' => string '1' (length=1)
public 'date' => string '2015-06-08 14:41:50' (length=19)
然后我有一些扩展这个模型的类:
class Company extends Model {
public function setClient (Client $cli) {
//do stuff
}
public function setName($name) {
//do stuff
}
public function setAge($age) {
//do stuff
}
}
//here I use the class
$myCompany = new Company();
$myCompany->find();
方法 setName()
和 setAge()
工作正常,但 setClient()
返回
Catchable fatal error: Argument 1 passed to setClient() must be an instance of Client, string given
简而言之,我如何处理PHP中的动态方法和输入?寻求帮助,我发现了一些关于 Reflection 的信息,但我以前从未使用过此类,而且我发现的示例对我没有帮助,尽管我认为这是正确的方法。
有没有人有过类似的经历?
更新
我试图在我的问题中加入一些代码来澄清我的问题。
最佳答案
如果我正确理解你的问题,你想检查方法需要什么类型提示。我从 the docs 中获取了这段代码片段.
<?php
//Target our class
$reflector = new ReflectionClass('MyClass');
//Get the parameters of a method
$parameters = $reflector->getMethod('FireCannon')->getParameters();
//Loop through each parameter and get the type
foreach($parameters as $param)
{
//Before you call getClass() that class must be defined!
echo $param->getClass()->name;
}
将上面的代码添加到您的代码中。这将检查方法中的参数是否是一个对象,如果是一个对象,它将检查该类是否与给定的值相同。如果参数不是对象,您可以直接调用该方法,或者如果您对非对象参数进行类型提示,您也可以检查它们是否相等。这段代码绝对应该修改和清理,但我认为这足以表达我的观点。
class Model {
public function find () {
$nameClass = get_called_class();
$instance = new $nameClass;
$result = $this->conn->select();//the select method returns a SQL SELECT from the database
if (!isset($result[0])) {
throw new Exception('You have a error in your class');
}
foreach ($result[0] as $key => $val) {
$methodProp = $this->getMethod($key);
$reflector = new ReflectionClass($nameClass);
$reflectorMethod = $reflector->getMethod($methodProp);
// Make sure method doesn't require more than one param,
// and it has defined at least one param.
if ($reflectorMethod->getNumberOfRequiredParameters() > 1 ||
$reflectorMethod->getNumberOfParameters() < 1
) {
// Throw error
}
//Get the parameters of a method
$parameters = $reflectorMethod->getParameters();
if (is_object($parameters[0])) {
if (!is_object($val) {
// Throw error
}
//Before you call get_class() that class must be defined!
//Use either class_exists($nameClass); or
//in_array($nameClass, get_declared_classes()); to check.
if ($val->getClass()->name !== get_class($parameters[0])) {
// Throw error
}
} else if (gettype($parameters[0]) !== gettype($val)) {
// Throw error
}
$instance->$methodProp($val);
}
}
}
关于php - 在 PHP 中输入参数的动态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30811105/
此时,程序显示两种选择并打印一条语句,表明用户赢了、计算机赢了还是平局。继续玩直到用户选择停止,然后打印用户赢的次数,损失, 和关系. import java.util.Scanner; import
我想在 d3.js 中制作一个条形图,每个项目或行都有正条和负条,如下所示: 它有点像谷歌金融“行业摘要”图表(http://google.com/finance) 任何人都可以指出我这种图表的 d3
尝试根据 GF 和 GA 创建一个新列“Results”。想知道如何通过定义函数和条件语句来做到这一点。以下是我对一行的初步尝试,但无法弄清楚如何将其应用于所有行,非常感谢您的帮助! 日期地点对手GF
我对 Java 还很陌生,并且有一项任务被困住了。我相信我的 boolean 函数是正确的,但是我不知道在主函数中要写什么。 这是作业: 编写一个公共(public)函数(静态方法)winner(in
使用掷骰子游戏中的 10,000 次模拟来计算获胜概率 (wins/(wins + Loss))。下面是双骰子游戏的方法: public class CrapsGame { public st
我是一名优秀的程序员,十分优秀!