如何从 parent 中的 filter_var 回调调用 child 方法
How to call a child method from filter_var callback in parent
我有一个 child class 拥有一堆私人财产。 parent class 有 __SET
和 __GET
方法。
parent __SET
方法有 filter_var($var, FILTER_CALLBACK, ['options' => 'childMethod');
但总是失败并显示错误 filter_var(): First argument is expected to be a valid callback
查看下面的示例代码,您可以将其复制并粘贴到 http://phpfiddle.org/lite/
对于 "childMethod" 我试过使用:
$this->childMethod()
$this->childMethod
this->childMethod
this->childMethod()
childMethod()
childMethod
全部错误
<?php
abstract class a {
public function callChild() {
$var = $this->iAmChild();
echo $var."<br />";
echo filter_var($var, FILTER_CALLBACK, ["options" => 'this->callback'])."<br />";
echo $this->callback($var)."<br />";
}
// abstract function callback();
}
class b extends a {
public function iAmChild() {
return "I am the child function";
}
public function callback($value) {
$value = strtoupper($value);
return $value;
}
}
$child = new b();
$child->callChild();
function convertSpace($string)
{
return str_replace(" ", "_", $string);
}
$string = "Peter is a great guy!";
echo filter_var($string, FILTER_CALLBACK,["options"=>"convertSpace"]);
?>
可能吗?
每个childclass代表一个数据库table,所以我想过滤控制"setting"的属性。每个 child class 都有属性列表,以及一个或多个提供 FILTER_*
类型的数组以供每个 属性 使用。这适用于除 FILTER_CALLBACK
.
以外的所有人
如何将 child 方法作为回调函数传递给 filter_var
?
评论中提到的答案是将第 6 行代码从
echo filter_var($var, FILTER_CALLBACK, ["options" => 'this->callback'])."<br />";
至
echo filter_var($var, FILTER_CALLBACK, ["options" => [$this, 'callback'])."<br />";
我有一个 child class 拥有一堆私人财产。 parent class 有 __SET
和 __GET
方法。
parent __SET
方法有 filter_var($var, FILTER_CALLBACK, ['options' => 'childMethod');
但总是失败并显示错误 filter_var(): First argument is expected to be a valid callback
查看下面的示例代码,您可以将其复制并粘贴到 http://phpfiddle.org/lite/
对于 "childMethod" 我试过使用:
$this->childMethod()
$this->childMethod
this->childMethod
this->childMethod()
childMethod()
childMethod
全部错误
<?php
abstract class a {
public function callChild() {
$var = $this->iAmChild();
echo $var."<br />";
echo filter_var($var, FILTER_CALLBACK, ["options" => 'this->callback'])."<br />";
echo $this->callback($var)."<br />";
}
// abstract function callback();
}
class b extends a {
public function iAmChild() {
return "I am the child function";
}
public function callback($value) {
$value = strtoupper($value);
return $value;
}
}
$child = new b();
$child->callChild();
function convertSpace($string)
{
return str_replace(" ", "_", $string);
}
$string = "Peter is a great guy!";
echo filter_var($string, FILTER_CALLBACK,["options"=>"convertSpace"]);
?>
可能吗?
每个childclass代表一个数据库table,所以我想过滤控制"setting"的属性。每个 child class 都有属性列表,以及一个或多个提供 FILTER_*
类型的数组以供每个 属性 使用。这适用于除 FILTER_CALLBACK
.
如何将 child 方法作为回调函数传递给 filter_var
?
评论中提到的答案是将第 6 行代码从
echo filter_var($var, FILTER_CALLBACK, ["options" => 'this->callback'])."<br />";
至
echo filter_var($var, FILTER_CALLBACK, ["options" => [$this, 'callback'])."<br />";