相关疑难解决方法(0)

PHP:如何使用类函数作为回调

我有一个类,我想用作回调的方法.如何将它们作为参数传递?

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不可用)

php oop callback

80
推荐指数
4
解决办法
6万
查看次数

array_map不在类中工作

我正在尝试创建一个类来处理数组,但我似乎无法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 arrays class function array-map

43
推荐指数
2
解决办法
4万
查看次数

PHP - 计算字符串中的逗号数

如何计算逗号在字符串中出现的次数?

A B C D

它应该返回"3"

php

7
推荐指数
2
解决办法
1万
查看次数

PHP中的递归stripslashes方法疑难解答

我试图在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的非引用方法.

php

1
推荐指数
1
解决办法
1136
查看次数

标签 统计

php ×4

array-map ×1

arrays ×1

callback ×1

class ×1

function ×1

oop ×1