PHP关联数组:值的唯一函数
因此,由于一些棘手的框架问题,我有点被困在这里。基本上,我从用户输入中获取一个字符串,并用我的模型中的值替换它,该值只有在调用时才知道 我的结构是这样的:PHP关联数组:值的唯一函数,php,associative-array,Php,Associative Array,因此,由于一些棘手的框架问题,我有点被困在这里。基本上,我从用户输入中获取一个字符串,并用我的模型中的值替换它,该值只有在调用时才知道 我的结构是这样的: static public $placeholders = array("[REPLACE_STRING_1]"=>'firstName', "[REPLACE_STRING_2]"=>'DateTime->format(\'aa,bb\')'); //
static public $placeholders = array("[REPLACE_STRING_1]"=>'firstName',
"[REPLACE_STRING_2]"=>'DateTime->format(\'aa,bb\')');
//Problem line above. Very messy, format is an arm long and painful
public function myfunction(ModelClass $master) {
$body = $this->sampleService->get('samplebody'); // populate body from DB
foreach(static::$placeholders as $key => $value){
$body = str_replace($key, $master->value, $body);
}
return $body;
}
这就产生了一些非常丑陋的代码。我的老板想编辑它,使一个函数成为数组的一部分,分配给每个条目,运行它来过滤/编辑代码。差不多
function dateFormat($prop){
return $prop->format('aa,bb');
}
function asIs($prop){
return $prop;
}
static public $placeholders = array("[REPLACE_STRING_1]"=>['firstName', asIs]
"[REPLACE_STRING_2]"=>['DateTime', dateFormat]);
PHP中是否有任何现有的结构或函数使这成为可能,或者他的代码愿望只是一个空想
编辑:我想出了一个与下面的答案非常相似的解决方案,但需要进行一些修改来传递变量
function dateFormat ($prop, $master) {
return $master->$prop->format('aabb');
}
function asIs ($prop, $master) {
return $appointment->$prop;
}
static public $placeholders = array("[REPLACE_STRING_1]"=>['firstname','asIs'],
"[REPLACE_STRING_2]"=>['DateTime', dateFormat];
//instatiate service to get sampleService values
//main difference here
public function buildBody(ModelClass $master) {
$body = $this->sampleService->get('samplebody');
foreach(static::$placeholders as $key => $value){
$body = preg_replace_callback('/'.preg_quote($key).'/',
//Use closure here to pass in $master, otherwise laravel gets angry about strings instead of objects
function ($matches) use ($master) {
$placeholder = static::placeholders[$matches[0]];
$func = $placeholder[1];
$prop = $placeholder[0];
return call_user_func(array($this, $func), $prop, $appointment);
}, $body);
}
return $body;
}
总而言之,这对我来说是一个非常有趣的问题,我正试图找到一种方法来进一步解决它。我将把你的答案标记为正确,因为这对我来说非常有帮助。如果我答对了问题,我将使用
preg\u replace\u callback
并这样做:
function dateFormat($prop){
return $prop;
}
function asIs($prop){
return $prop;
}
static public $placeholders = array(
"[REPLACE_STRING_1]"=>['firstName', 'asIs'],
"[REPLACE_STRING_2]"=>['DateTime', 'dateFormat']
);
private function replace($matches) {
$placeholder = static::$placeholders[$matches[0]];
$func = $placeholder[1];
$prop = $placeholder[0];
// this calls your class methods ('asIs', 'dateFormat')
// with $prop = 'firstName' etc.
return call_user_func(array($this, $func), $prop);
}
public function myfunction(ModelClass $master) {
$body = $this->sampleService->get('samplebody');
foreach(static::$placeholders as $key => $value) {
// use a callback replace method instead of str_replace
$body = preg_replace_callback('/' . preg_quote($key) . '/', array($this, 'replace'), $body);
}
return $body;
}
你试过你老板想要的吗?结果是什么?目前,这是一个语法错误。我不确定是否有办法将函数应用于关联数组内联声明。请告诉您的上司,PHP无法实现这一点。你需要有一个getter方法来调用函数并返回结果-在调用时无法完成我仍然不太清楚你在追求什么,但我的蜘蛛感觉是你应该研究call\u user\u func或call\u user\u func\u数组来动态调用函数并得到结果。我今天将尝试一下,看看它是否有效,但那应该是我要找的。谢谢非常接近工作,但遇到了障碍。当我调用DateFormat时,它需要是$master->$value->format(),这是由于laravel的限制。似乎如果我在传入之前将值[0]设置为$master-$value[0],它仍然认为它是字符串类型,而不是从$master类型继承。您可以尝试将
$master
添加到myfunction静态中的占位符::$placeholders['REPLACE_string_2'][2]=$master
并在函数替换中
将其作为另一个参数传递给函数日期格式($prop,$master)
(返回调用用户函数(数组($this,$func),$prop,$placeholder[2]);
)以回答您的原因,因为master是buildbody的一个参数,所以这不起作用,它是包含要替换为模板的数据的模型实例。所以我不能把它放在静态对象中。但是感谢您的帮助。您可以将另一个属性private$master
添加到类中,并将变量设置为:$this->master=$master
,或者作为参考:$this->master=&$master
,并在函数中使用它。