PHP向函数添加方法
是否可以在函数中添加方法?
例如:
<?
function func(){
;
}
//add method
func->test = function(){
;
}
func->test();
func();
我来自 javascript 背景,因此我习惯了“一切都是对象”。
编辑:
我只是在解释新的 phpers 的误解通常来自哪里。我知道上面的代码不起作用。
编辑2
弄清楚了。
class myfunc_class{
function __invoke(){
//function body
}
function __call($closure, $args)
{
call_user_func_array($this->$closure, $args);
}
}
$func = new myfunc_class;
$func->test = function(){
echo '<br>test<br>';
};
$func->test();
$func();
甚至更性感:)
class func{
public $_function;
function __invoke(){
return call_user_func_array($this->_function,func_get_args());
}
function __construct($fun){
$this->_function = $fun;
}
function __call($closure, $args)
{
call_user_func_array($this->$closure, $args);
}
}
$func = new func(function($value){
echo $value;
});
$func->method = function(){
echo '<br>test<br>';
};
$func('someValue');
$func->method();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,
在 PHP 中并非所有东西都是对象。事实上,唯一的物体就是物体。更具体地说,一般来说,是类的实例化。
您的代码转换为 PHP
在其他代码中,您可以像这样使用它:
另外: 阅读更多关于 PHP 和 PHP面向对象编程
No.
Not everything is an object in PHP. In fact the only thing that is an object is, well, an object. More specifically, and generally, an instantiation of a class.
Your code converted to PHP
In other code you would use it like this:
Also: read more about PHP & OOP
不,因为对象是与函数不同的 PHP 语言构造。函数没有属性,而只是执行指令。
但是,如果
func
是一个预定义的类,那么是的......用一点巫术,无视公众的抗议,放弃可读性和 PHP 编码标准,并使用 闭包 与__call()
魔术方法...如果没有
__call()
,这将无法像您想象的那样工作,因为通过$obj->test(1,1)
, PHP 认为你正在尝试当超出对象范围时调用func
不存在的方法。但在内部,新的"test"
属性的类型为:闭包,call_user_func_array()
只是将"test"
属性视为另一个函数,因此您可以向外界隐藏这一点诡计 范围。No, because an object is a different PHP language construct than a function. Functions do not have properties, but are instead simply execution instructions.
But, if
func
were instead a pre-defined class, then yes... with a bit of witchcraft, ignoring public outcry, foregoing readability and PHP coding standards, and by using closures with the__call()
magic method...This won't work as you'd think without
__call()
, because by$obj->test(1,1)
, PHP thinks you're trying to call a non-existent method offunc
when out of object scope. But inside, being that the new"test"
property is of a type: closure, thecall_user_func_array()
just sees the"test"
property as just another function, so you can hide this bit of trickery from outside scope.您需要函数
func()
返回一个对象,然后您就可以执行以下操作:func()->test();
但请请注意,您在 PHP 中处理对象的方式不正确,我建议您阅读 OO 文档 这里。
You would need your function
func()
to return an object, then you'd be able to do something like:func()->test();
But please note that your way of handling objects is not right in PHP and I suggest that you go read the OO documentations here.
与 javacript 不同,在 PHP 中并不是所有东西都是对象。因此,您需要区分函数和类。
如果你想创建一个对象,你需要首先定义类。
然后,您可以根据需要向类添加任意数量的函数。但您需要首先定义它们:
当一切准备就绪后,您就可以将其付诸实践:
查看手册了解更多信息。
In difference to javacript, in PHP not everything is an object. Therefore you need to differ between function and class.
If you want to create an object, you need to define the class first.
You can then add as many functions to the class as you need. But you need to define them first:
When everything is ready, you can bring it to life then:
Checkout the manual for more.
您无法执行您想要执行的操作,但您可以在其他函数内部定义函数。
此示例输出文本:
此示例输出错误:
You can't do what you're trying to do, but you can define functions inside of other functions.
This example outputs text:
This example outputs an error: