如何在 array_map 的可调用对象中实现类方法

发布于 2024-08-23 05:17:48 字数 1046 浏览 17 评论 0原文

我正在尝试创建一个类来处理数组,但我似乎无法让 array_map() 在其中工作。

$array = [1,2,3,4,5,6,7,8,9,10];
class test {
    public $values;

    public function adding($data) {
        $this->values = array_map($this->dash(), $data);
    }

    public function dash($item) {
        return '-' . $item . '-';
    }

}

var_dump($array);

$test = new test();
$test->adding($array);

// Expected: -1-,-2-,-3-,-4-... 
var_dump($test->values);

输出

array(10) { [0]=>; int(1) [1]=> int(2) [2]==> int(3) [3]==> int(4) [4]==> int(5) [5]==> int(6) [6]==> int(7) [7]==> int(8) [8]==> int(9)[9]==> int(10) }

Warning: Missing argument 1 for test::dash(), called in [...]\arraytesting.php on line 11 and defined in [...]\arraytesting.php on line 15

Warning: array_map() expects parameter 1 to be a valid callback, function '--' not found or invalid function name in [...]\arraytesting.php on line 11 NULL

我做错了什么或者这个函数在类中不起作用?

I am trying to create a class to handle arrays but I can't seem to get array_map() to work in it.

$array = [1,2,3,4,5,6,7,8,9,10];
class test {
    public $values;

    public function adding($data) {
        $this->values = array_map($this->dash(), $data);
    }

    public function dash($item) {
        return '-' . $item . '-';
    }

}

var_dump($array);

$test = new test();
$test->adding($array);

// Expected: -1-,-2-,-3-,-4-... 
var_dump($test->values);

This outputs

array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) [9]=> int(10) }

Warning: Missing argument 1 for test::dash(), called in [...]\arraytesting.php on line 11 and defined in [...]\arraytesting.php on line 15

Warning: array_map() expects parameter 1 to be a valid callback, function '--' not found or invalid function name in [...]\arraytesting.php on line 11 NULL

What am I doing wrong or does this function just not work inside classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

不甘平庸 2024-08-30 05:17:48

您以错误的方式指定 dash 作为回调。

这是行不通的:

$this->classarray = array_map($this->dash(), $data);

这是:

$this->classarray = array_map([$this, 'dash'], $data);

阅读回调可能采取的不同形式 在这里

You are specifying dash as the callback in the wrong way.

This does not work:

$this->classarray = array_map($this->dash(), $data);

This does:

$this->classarray = array_map([$this, 'dash'], $data);

Read about the different forms a callback may take here.

雨落□心尘 2024-08-30 05:17:48

当使用类方法作为 array_map()usort() 等函数的回调时,您必须将回调作为二值数组发送。第二个值始终是字符串形式的方法名称。第一个值是上下文(类名或对象)

// Static outside of class context
array_map( array( 'ClassName', 'methodName' ), $array );

// Static inside class context
array_map( array( __CLASS__, 'methodName' ), $array );

// Non-static outside of object context
array_map( array( $object, 'methodName' ), $array );

// Non-static inside of object context
array_map( array( $this, 'methodName' ), $array );

When using a class method as a callback for functions like array_map() and usort(), you have to send the callback as two-value array. The 2nd value is always the name of the method as a string. The 1st value is the context (class name or object)

// Static outside of class context
array_map( array( 'ClassName', 'methodName' ), $array );

// Static inside class context
array_map( array( __CLASS__, 'methodName' ), $array );

// Non-static outside of object context
array_map( array( $object, 'methodName' ), $array );

// Non-static inside of object context
array_map( array( $this, 'methodName' ), $array );
不醒的梦 2024-08-30 05:17:48

array_map($this->dash(), $data) 使用 0 个参数调用 $this->dash() 并使用返回值作为回调函数适用于数组的每个成员。您需要使用 array_map(array($this,'dash'), $data) 来代替。

array_map($this->dash(), $data) calls $this->dash() with 0 arguments and uses the return value as the callback function to apply to each member of the array. You want array_map(array($this,'dash'), $data) instead.

旧伤慢歌 2024-08-30 05:17:48

它必须读取

$this->classarray = array_map(array($this, 'dash'), $data);

array -thing 是 对象实例方法的 PHP 回调。对常规函数的回调定义为包含函数名称 ('functionName') 的简单字符串,而静态方法调用则定义为 array('ClassName, 'methodName') 或像这样的字符串:'ClassName::methodName'(从 PHP 5.2.3 开始有效)。

It must read

$this->classarray = array_map(array($this, 'dash'), $data);

The array-thing is the PHP callback for a object instance method. Callbacks to regular functions are defined as simple strings containing the function name ('functionName'), while static method calls are defined as array('ClassName, 'methodName') or as a string like that: 'ClassName::methodName' (this works as of PHP 5.2.3).

赏烟花じ飞满天 2024-08-30 05:17:48

array_map 接受回调 作为它的第一个参数。

静态方法的回调是这样写的:

array('classname', 'methodname')

这意味着,在您的具体情况下,您将使用:

array_map(array('stripSlashesRecursive', ''), $value);

有关回调的更多信息,请参阅 PHP 手册的这一部分:本文档中使用的伪类型和变量 - 回调

array_map takes a callback as its first parameter.

And a callback to a static method is written like this :

array('classname', 'methodname')

Which means that, in your specific case, you'd use :

array_map(array('stripSlashesRecursive', ''), $value);

For more informations about callbacks, see this section of the PHP manual : Pseudo-types and variables used in this documentation - callback.

深海不蓝 2024-08-30 05:17:48

如果类属于不同的命名空间,则需要使用完整的命名空间类名。下面是使用 CakePHP 实用程序类的示例:

这不会工作:

array_map(array('Inflector', 'humanize'), $some_array));

这会工作:

array_map(array('Cake\Utility\Inflector', 'humanize'), $some_array));

In case the class belongs to a different namespace, you need to use the complete namespaced class name. Below is an example using a CakePHP Utility class:

This will not work:

array_map(array('Inflector', 'humanize'), $some_array));

This will work:

array_map(array('Cake\Utility\Inflector', 'humanize'), $some_array));
旧城烟雨 2024-08-30 05:17:48
array_map( array('Sanitize', 'stripSlashesRecursive'), $value) ...
array_map( array('Sanitize', 'stripSlashesRecursive'), $value) ...
谁把谁当真 2024-08-30 05:17:48

//常规函数:
array_map('MyFunction', $array);

//类中的静态函数:
array_map(array('MyClass', 'MyFunction'), $array);

//来自对象的函数:
array_map(array($this, 'MyFunction'), $array);

//来自父类的函数
array_map(array($this, 'parent::MyFunction'), $array);

//Regular functions:
array_map('MyFunction', $array);

//static functions in a class:
array_map(array('MyClass', 'MyFunction'), $array);

//functions from an object:
array_map(array($this, 'MyFunction'), $array);

//functions from an parent class
array_map(array($this, 'parent::MyFunction'), $array);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文