如何在codeigniter中使用回调函数
How to use callback function in codeigniter
我有一个数组 $services,这个数组包含以下值:
Array
(
[0] => One way
[1] => Hourly
[2] => To Airport
[3] => From Airport
[4] => Birthday
[5] => Wedding
[6] => Concert
[7] => Sporting Event
[8] => Cruise Party
[9] => Funeral
)
您可以注意到数组中的某些值包含 space.
为了从数组值中删除这个 space 我创建了一个 array_walk 函数,它进入数组并且 trim 白色 space.
public function trim_value(&$value) {
$value = trim($value);
}
众所周知,array_walk函数的语法是:
array_walk($array, 'callback_function');
现在我想在 codeigniter 控制器中使用这个函数。据我所知,控制器中的一个函数被用作另一个函数中的 $this->function。
所以我尝试将回调函数用作:
array_walk($services, $this->trim_value);
它总是抛出以下错误:
A PHP Error was encountered
<p>Severity: Notice</p>
<p>Message: Undefined property: Attribute::$trim_value</p>
<p>Filename: controllers/attribute.php</p>
<p>Line Number: 230</p>
那么有人能告诉我如何在控制器的一个函数中使用回调函数吗?
您必须传递 array_walk()
一个数组,其中 $this
作为已定义的 variable/pointer
。
试试这个
array_walk($array, array($this, 'trim_value'));
我有一个数组 $services,这个数组包含以下值:
Array
(
[0] => One way
[1] => Hourly
[2] => To Airport
[3] => From Airport
[4] => Birthday
[5] => Wedding
[6] => Concert
[7] => Sporting Event
[8] => Cruise Party
[9] => Funeral
)
您可以注意到数组中的某些值包含 space.
为了从数组值中删除这个 space 我创建了一个 array_walk 函数,它进入数组并且 trim 白色 space.
public function trim_value(&$value) {
$value = trim($value);
}
众所周知,array_walk函数的语法是:
array_walk($array, 'callback_function');
现在我想在 codeigniter 控制器中使用这个函数。据我所知,控制器中的一个函数被用作另一个函数中的 $this->function。
所以我尝试将回调函数用作:
array_walk($services, $this->trim_value);
它总是抛出以下错误:
A PHP Error was encountered
<p>Severity: Notice</p> <p>Message: Undefined property: Attribute::$trim_value</p> <p>Filename: controllers/attribute.php</p> <p>Line Number: 230</p>
那么有人能告诉我如何在控制器的一个函数中使用回调函数吗?
您必须传递 array_walk()
一个数组,其中 $this
作为已定义的 variable/pointer
。
试试这个
array_walk($array, array($this, 'trim_value'));