我有一个类,我想用作回调的方法.如何将它们作为参数传递?
Class MyClass {
public function myMethod() {
$this->processSomething(this->myCallback); // How it must be called ?
$this->processSomething(self::myStaticCallback); // How it must be called ?
}
private function processSomething(callable $callback) {
// process something...
$callback();
}
private function myCallback() {
// do something...
}
private static function myStaticCallback() {
// do something...
}
}
Run Code Online (Sandbox Code Playgroud)
UPD:如何从static方法中做同样的事情(什么时候$this不可用)
我正在尝试创建一个类来处理数组,但我似乎无法array_map()在其中工作.
<?php
//Create the test array
$array = array(1,2,3,4,5,6,7,8,9,10);
//create the test class
class test {
//variable to save array inside class
public $classarray;
//function to call array_map function with the given array
public function adding($data) {
$this->classarray = array_map($this->dash(), $data);
}
// dash function to add a - to both sides of the number of the input array
public function dash($item) {
$item2 = '-' . $item . '-';
return $item2;
}
}
// dumps start array …Run Code Online (Sandbox Code Playgroud) 我试图在PHP文档中使用stripslashes_deep函数作为方法,但是当我将它传递给数组时,它不起作用.但是,它确实可以使用字符串.任何人都可以指出为什么它可能无法正常工作?魔术引号是因为我正在测试这种可能性,但就像我说的那样,它适用于字符串而且我也没有测试$ _POST或$ _GET数据.这是代码:
protected function stripslashes_deep($input){
return is_array($input) ? array_map($this->stripslashes_deep, $input) : stripslashes($input);
}
Run Code Online (Sandbox Code Playgroud)
字符串'O \'Reilly'被转换为'O'Reilly',但下面的数组保持不变:
数组([0] =>数组([userId] => 23 [0] => 23 [用户名] => O \'Reilly [1] => O \'Reilly [userFirstName] => Bill [2] => Bill [userLastName] => O \'Reilly [3] => O \'Reilly))
任何帮助将不胜感激.
编辑:
该数组来自数据库并具有斜杠,因为它是由PDO对象插入的绑定参数.我扩展了PDO,我试图在获取数组时自动去除斜杠,所以我不必每次输出数据时调用stripslashes.如果有人知道更好的方法,我肯定会接受建议.我没有看到任何类型的PDO的非引用方法.