PHP向函数添加方法

发布于 2024-11-16 06:48:04 字数 1198 浏览 0 评论 0 原文

是否可以在函数中添加方法?

例如:

<?
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();

Is it possible to add methods to functions?

For example:

<?
function func(){
    ;
}
//add method
func->test = function(){
    ;
}

func->test();
func();

I'm coming from a javascript background, and therefore I'm used to 'everything is an object'.

EDIT:

I was just explaining where the misconception may often come from for new phpers. I understand the above code doesn't work.

EDIT 2

Figured it out.

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();

Even sexier :)

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 技术交流群。

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

发布评论

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

评论(5

寻找我们的幸福 2024-11-23 06:48:04

不,

在 PHP 中并非所有东西都是对象。事实上,唯一的物体就是物体。更具体地说,一般来说,是类的实例化。

您的代码转换为 PHP

// function_object.php
<?php

class FunctionObject {

   public method func() {
       // do stuff

   }

}

?>

在其他代码中,您可以像这样使用它:

<?php

// example.php in same folder as function_object.php
include 'function_object.php';

$FuncObj = new FunctionObject;
$FuncObj->func();

另外: 阅读更多关于 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

// function_object.php
<?php

class FunctionObject {

   public method func() {
       // do stuff

   }

}

?>

In other code you would use it like this:

<?php

// example.php in same folder as function_object.php
include 'function_object.php';

$FuncObj = new FunctionObject;
$FuncObj->func();

Also: read more about PHP & OOP

治碍 2024-11-23 06:48:04

不,因为对象是与函数不同的 PHP 语言构造。函数没有属性,而只是执行指令。

但是,如果 func 是一个预定义的类,那么是的......用一点巫术,无视公众的抗议,放弃可读性和 PHP 编码标准,并使用 闭包__call()魔术方法...

class func
{

    function __call($func, $args)
    {
        return call_user_func_array($this->$func, $args);
    }

}


$obj = new func;

$obj->test = function($param1, $param2)
{
    return $param1 + $param2;
};

echo $obj->test(1,1);

如果没有__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...

class func
{

    function __call($func, $args)
    {
        return call_user_func_array($this->$func, $args);
    }

}


$obj = new func;

$obj->test = function($param1, $param2)
{
    return $param1 + $param2;
};

echo $obj->test(1,1);

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 of func when out of object scope. But inside, being that the new "test" property is of a type: closure, the call_user_func_array() just sees the "test" property as just another function, so you can hide this bit of trickery from outside scope.

jJeQQOZ5 2024-11-23 06:48:04

您需要函数 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.

云淡风轻 2024-11-23 06:48:04

与 javacript 不同,在 PHP 中并不是所有东西都是对象。因此,您需要区分函数和类。

如果你想创建一个对象,你需要首先定义类。

class myClass {
}

然后,您可以根据需要向类添加任意数量的函数。但您需要首先定义它们:

class myClass {
  function test() {
    echo "test!\n";
  }
}

当一切准备就绪后,您就可以将其付诸实践:

$class = new myClass;
$class->test();

查看手册了解更多信息

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.

class myClass {
}

You can then add as many functions to the class as you need. But you need to define them first:

class myClass {
  function test() {
    echo "test!\n";
  }
}

When everything is ready, you can bring it to life then:

$class = new myClass;
$class->test();

Checkout the manual for more.

滴情不沾 2024-11-23 06:48:04

您无法执行您想要执行的操作,但您可以在其他函数内部定义函数。

此示例输出文本:

function a() {
    function b() { echo 'Hi'; }
}
a();
b();
Output: HiHi

此示例输出错误:

function a() {
    function b() { echo 'Hi'; }
}
b();
Output: ERROR

You can't do what you're trying to do, but you can define functions inside of other functions.

This example outputs text:

function a() {
    function b() { echo 'Hi'; }
}
a();
b();
Output: HiHi

This example outputs an error:

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