PHP:使用方法作为回调 [英] PHP: Using a method as a callback

查看:66
本文介绍了PHP:使用方法作为回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用array_walk_recursive做某事,并且想使用类的方法之一作为回调,所以尝试:

I was trying to use array_walk_recursive for something, and wanted to use one of the class' methods as the call back, so trying:

  array_walk_recursive($TAINTED, "$this->encode()");

及其变体都失败了.我最终选择了:

and variations thereof all failed. I eventually settled for:

array_walk_recursive($TAINTED, 'className::encode');

可行,但是我在这里已经读过,以这种静态方式调用类方法通常被认为是不好的做法.还是这是必要的情况之一?

which works, but I've read on here that calling class methods in a static fashion like this is often considered poor practice. Or is this one of those situations where it's necessary?

那么,这是正确的方法吗?还是有一种方法可以放入回调函数而不必依赖于将其用作静态类方法?

So, is this the right way to go about it, or is there a way to put in the callback function without having to fall back on using it as a static class method?

推荐答案

array_walk_recursive($TAINTED, array($this, 'encode'));

这篇关于PHP:使用方法作为回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆