如何从存储在变量中的字符串调用函数?

发布于 2024-07-24 10:30:54 字数 261 浏览 5 评论 0原文

我需要能够调用函数,但函数名称存储在变量中,这可能吗? 例如:

function foo ()
{
    //code here
}

function bar ()
{
    //code here
}

$functionName = "foo";
// I need to call the function based on what is $functionName

I need to be able to call a function, but the function name is stored in a variable, is this possible? e.g:

function foo ()
{
    //code here
}

function bar ()
{
    //code here
}

$functionName = "foo";
// I need to call the function based on what is $functionName

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

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

发布评论

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

评论(18

抱着落日 2024-07-31 10:30:55

以下代码可以帮助在 PHP 中编写动态函数。
现在函数名称可以通过变量“$current_page”动态更改。

$current_page = 'home_page';
$function = @${$current_page . '_page_versions'};
$function = function() {
    echo 'current page';
};
$function();

Following code can help to write dynamic function in PHP.
now the function name can be dynamically change by variable '$current_page'.

$current_page = 'home_page';
$function = @${$current_page . '_page_versions'};
$function = function() {
    echo 'current page';
};
$function();
娜些时光,永不杰束 2024-07-31 10:30:55

考虑到这里给出的一些优秀答案,有时您需要精确。
例如。

  1. 如果函数有返回值,例如 (boolean,array,string,int,float
    ETC)。
  2. 如果函数没有返回值,请检查
  3. 该函数是否存在

让我们看看它对给出的一些答案的贡献。

Class Cars{
        function carMake(){
        return 'Toyota';
        }
        function carMakeYear(){
        return 2020;
        }
        function estimatedPriceInDollar{
        return 1500.89;
        }
        function colorList(){
        return array("Black","Gold","Silver","Blue");
        }
        function carUsage(){
        return array("Private","Commercial","Government");
        }
    function getCar(){
    echo "Toyota Venza 2020 model private estimated price is 1500 USD";
    }
 }

我们想要检查方法是否存在并动态调用它。

$method = "color List";
        $class = new Cars();
        //If the function have return value;
        $arrayColor = method_exists($class, str_replace(' ', "", $method)) ? call_user_func(array($this, $obj)) : [];
        //If the function have no return value e.g echo,die,print e.t.c
     $method = "get Car";
        if(method_exists($class, str_replace(' ', "", $method))){
        call_user_func(array($class, $method))
        }

谢谢

Considering some of the excellent answers given here, sometimes you need to be precise.
For example.

  1. if a function has a return value eg (boolean,array,string,int,float
    e.t.c).
  2. if the function has no return value check
  3. if the function exists

Let's look at its credit to some of the answers given.

Class Cars{
        function carMake(){
        return 'Toyota';
        }
        function carMakeYear(){
        return 2020;
        }
        function estimatedPriceInDollar{
        return 1500.89;
        }
        function colorList(){
        return array("Black","Gold","Silver","Blue");
        }
        function carUsage(){
        return array("Private","Commercial","Government");
        }
    function getCar(){
    echo "Toyota Venza 2020 model private estimated price is 1500 USD";
    }
 }

We want to check if method exists and call it dynamically.

$method = "color List";
        $class = new Cars();
        //If the function have return value;
        $arrayColor = method_exists($class, str_replace(' ', "", $method)) ? call_user_func(array($this, $obj)) : [];
        //If the function have no return value e.g echo,die,print e.t.c
     $method = "get Car";
        if(method_exists($class, str_replace(' ', "", $method))){
        call_user_func(array($class, $method))
        }

Thanks

少女七分熟 2024-07-31 10:30:55

我想到的一种非常规方法是,除非您通过一些自行编写的超级超自主人工智能生成整个代码,否则您想要“动态”调用的函数很有可能,已在您的代码库中定义。 那么为什么不直接检查字符串并跳出臭名昭著的 ifelse 舞蹈来召唤......你明白我的意思了。

例如。

if($functionName == 'foo'){
  foo();
} else if($functionName == 'bar'){
  bar();
}

如果您不喜欢 ifelse 梯子的平淡味道,甚至可以使用 switch-case

我理解,在某些情况下,“动态调用函数”是绝对必要的(就像一些自我修改的递归逻辑)。 但大多数日常琐碎用例都可以回避。

它消除了应用程序中的许多不确定性,同时在字符串与任何可用函数的定义不匹配时让您有机会执行后备函数。 恕我直言。

One unconventional approach, that came to my mind is, unless you are generating the whole code through some super ultra autonomous AI which writes itself, there are high chances that the functions which you want to "dynamically" call, are already defined in your code base. So why not just check for the string and do the infamous ifelse dance to summon the ...you get my point.

eg.

if($functionName == 'foo'){
  foo();
} else if($functionName == 'bar'){
  bar();
}

Even switch-case can be used if you don't like the bland taste of ifelse ladder.

I understand that there are cases where the "dynamically calling the function" would be an absolute necessity (Like some recursive logic which modifies itself). But most of the everyday trivial use-cases can just be dodged.

It weeds out a lot of uncertainty from your application, while giving you a chance to execute a fallback function if the string doesn't match any of the available functions' definition. IMHO.

眼中杀气 2024-07-31 10:30:55

我不知道为什么你必须使用它,对我来说听起来不太好,但如果只有少量函数,你可以使用 if/elseif 结构。
不知道是否可以直接解决。

就像是
$foo = "酒吧";
$测试=“foo”;
回显$$测试;

应该返回 bar,你可以尝试一下,但我认为这不适用于函数

I dont know why u have to use that, doesnt sound so good to me at all, but if there are only a small amount of functions, you could use a if/elseif construct.
I dont know if a direct solution is possible.

something like
$foo = "bar";
$test = "foo";
echo $$test;

should return bar, you can try around but i dont think this will work for functions

可是我不能没有你 2024-07-31 10:30:54

$functionName()call_user_func($functionName)

如果您需要提供存储在另一个变量中的参数(以数组的形式),使用 数组解包运算符

$function_name = 'trim';
$parameters = ['aaabbb','b'];
echo $function_name(...$parameters); // aaa

动态创建对象并调用其方法

$class = 'DateTime';
$method = 'format';
echo (new $class)->$method('d-m-Y');

或调用静态方法

$class = 'DateTime';
$static = 'createFromFormat';
$date = $class::$static('d-m-Y', '17-08-2023');

$functionName() or call_user_func($functionName)

If you need to provide parameters stored in another variable (in the form of array), use array unpacking operator:

$function_name = 'trim';
$parameters = ['aaabbb','b'];
echo $function_name(...$parameters); // aaa

To dynamically create an object and call its method use

$class = 'DateTime';
$method = 'format';
echo (new $class)->$method('d-m-Y');

or to call a static method

$class = 'DateTime';
$static = 'createFromFormat';
$date = $class::$static('d-m-Y', '17-08-2023');
爱本泡沫多脆弱 2024-07-31 10:30:54

我最喜欢的版本是内联版本:

${"variableName"} = 12;

$className->{"propertyName"};
$className->{"methodName"}();

StaticClass::${"propertyName"};
StaticClass::{"methodName"}();

您也可以将变量或表达式放在括号内!

My favorite version is the inline version:

${"variableName"} = 12;

$className->{"propertyName"};
$className->{"methodName"}();

StaticClass::${"propertyName"};
StaticClass::{"methodName"}();

You can place variables or expressions inside the brackets too!

梦里°也失望 2024-07-31 10:30:54

解决方案:使用 PHP7

注意: 有关汇总版本,请参阅答案末尾的 TL;DR。

旧方法

更新:此处解释的旧方法之一已被删除。 其他方法的解释可以参考其他答案,这里不再赘述。 顺便说一句,如果这个答案对您没有帮助,您应该返回升级您的东西。 PHP 5.6 支持已于 2019 年 1 月终止(现在甚至不支持 PHP 7.2 和 7.3)。 有关详细信息,请参阅支持的版本

正如其他人提到的,在 PHP5(以及 PHP7 等较新版本)中,我们可以使用变量作为函数名称,使用 call_user_func()call_user_func_array()

从 PHP7 开始,引入了新的方法:

注意: 括号内的所有内容都表示一个或多个表达式来形成某物,例如 表示形成函数名称的表达式。

动态函数调用:即时函数名称

我们可以一次性在括号内形成一个函数名称:

(<function_name>)(arguments);

例如:

function something(): string
{
    return "something";
}

$bar = "some_thing";

(str_replace("_", "", $bar))(); // something

// Possible, too; but generally, not recommended, because makes your
// code more complicated
(str_replace("_", "", $bar))()(); 

注意:尽管删除了str_replace() 不是错误,添加括号可以使代码更具可读性。 但是,有时您不能这样做,例如在使用 . 运算符时。 为了保持一致,我建议您始终添加括号。

动态函数调用:可调用属性

一个有用的示例是在对象的上下文中:如果您在属性中存储了可调用对象,则必须以这种方式调用它:

($object->{<property_name>})();

作为一个简单的示例:

// Suppose we're in a class method context
($this->eventHandler)();

显然,将其调用为 $this ->eventHandler() 是完全错误的:您的意思是调用名为 eventHandler 的方法。

动态方法调用:动态方法名称

就像动态函数调用一样,我们可以对方法调用执行相同的操作,用大括号而不是圆括号括起来(对于其他形式,请导航至 TL;DR 部分):

$object->{<method_name>}(arguments);
$object::{<method_name>}(arguments);

请参阅示例:

class Foo
{
    public function another(): string
    {
        return "something";
    }
}

$bar = "another thing";

(new Something())->{explode(" ", $bar)[0]}(); // something

动态方法调用:数组语法

PHP7 中添加的一种更优雅的方法如下:

[<object>, <method_name>](arguments);
[<class_name>, <method_name>](arguments); // Static calls only

作为示例:

class Foo
{
    public function nonStaticCall()
    {
        echo "Non-static call";
    }
    public static function staticCall()
    {
        echo "Static call";
    }
}

$x = new Foo();

[$x, "non" . "StaticCall"](); // Non-static call
[$x, "static" . "Call"](); // Static call

注意: 与前一种方法相比,使用此方法的好处是,您不必不关心调用类型(即是否是静态的)。

注意:如果您关心性能(和微优化),请不要使用此方法。 据我测试,这个方法确实比其他方法慢(10倍以上)。

额外示例:使用匿名类

让事情变得有点复杂,您可以使用 的组合匿名类以及上面的功能:

$bar = "SomeThing";

echo (new class {
    public function something()
    {
        return 512;
    }
})->{strtolower($bar)}(); // 512

TL;DR(结论)

一般来说,在PHP7中,使用以下形式都是可以的:

// Everything inside `<something>` brackets means one or more expressions
// to form something

// Dynamic function call via function name
(<function_name>)(arguments);

// Dynamic function call on a callable property
($object->{<property_name>})(arguments);

// Dynamic method call on an object
$object->{<method_name>}(arguments);
$object::{<method_name>}(arguments);

// Dynamic method call on a dynamically-generated object
(<object>)->{<method_name>}(arguments);
(<object>)::{<method_name>}(arguments);

// Dynamic method call, statically
ClassName::{<method_name>}(arguments);
(<class_name>)::{<method_name>}(arguments);

// Dynamic method call, array-like (no different between static
// and non-static calls
[<object>, <method_name>](arguments);

// Dynamic method call, array-like, statically
[<class_name>, <method_name>](arguments);

特别感谢这个 PHP 演讲

Solution: Use PHP7

Note: For a summarized version, see TL;DR at the end of the answer.

Old Methods

Update: One of the old methods explained here has been removed. Refer to other answers for explanation on other methods, it isn't covered here. By the way, if this answer doesn't help you, you should return upgrading your stuff. PHP 5.6 support has ended in January 2019 (now even PHP 7.2 and 7.3 are not being supported). See supported versions for more information.

As others mentioned, in PHP5 (and also in newer versions like PHP7) we could use variables as function names, use call_user_func() and call_user_func_array(), etc.

New Methods

As of PHP7, there are new ways introduced:

Note: Everything inside <something> brackets means one or more expressions to form something, e.g. <function_name> means expressions forming a function name.

Dynamic Function Call: Function Name On-the-fly

We can form a function name inside parentheses in just one go:

(<function_name>)(arguments);

For example:

function something(): string
{
    return "something";
}

$bar = "some_thing";

(str_replace("_", "", $bar))(); // something

// Possible, too; but generally, not recommended, because makes your
// code more complicated
(str_replace("_", "", $bar))()(); 

Note: Although removing the parentheses around str_replace() is not an error, putting parentheses makes code more readable. However, you cannot do that sometimes, e.g. while using . operator. To be consistent, I recommend you to put the parentheses always.

Dynamic Function Call: Callable Property

A useful example would be in the context of objects: If you have stored a callable in a property, you have to call it this way:

($object->{<property_name>})();

As a simple example:

// Suppose we're in a class method context
($this->eventHandler)();

Obviously, calling it as $this->eventHandler() is plain wrong: By that you mean calling a method named eventHandler.

Dynamic Method Call: Method Name On-the-fly

Just like dynamic function calls, we can do the same way with method calls, surrounded by curly braces instead of parentheses (for extra forms, navigate to TL;DR section):

$object->{<method_name>}(arguments);
$object::{<method_name>}(arguments);

See it in an example:

class Foo
{
    public function another(): string
    {
        return "something";
    }
}

$bar = "another thing";

(new Something())->{explode(" ", $bar)[0]}(); // something

Dynamic Method Call: The Array Syntax

A more elegant way added in PHP7 is the following:

[<object>, <method_name>](arguments);
[<class_name>, <method_name>](arguments); // Static calls only

As an example:

class Foo
{
    public function nonStaticCall()
    {
        echo "Non-static call";
    }
    public static function staticCall()
    {
        echo "Static call";
    }
}

$x = new Foo();

[$x, "non" . "StaticCall"](); // Non-static call
[$x, "static" . "Call"](); // Static call

Note: The benefit of using this method over the previous one is that, you don't care about the call type (i.e. whether it's static or not).

Note: If you care about performance (and micro-optimizations), don't use this method. As I tested, this method is really slower than other methods (more than 10 times).

Extra Example: Using Anonymous Classes

Making things a bit complicated, you could use a combination of anonymous classes and the features above:

$bar = "SomeThing";

echo (new class {
    public function something()
    {
        return 512;
    }
})->{strtolower($bar)}(); // 512

TL;DR (Conclusion)

Generally, in PHP7, using the following forms are all possible:

// Everything inside `<something>` brackets means one or more expressions
// to form something

// Dynamic function call via function name
(<function_name>)(arguments);

// Dynamic function call on a callable property
($object->{<property_name>})(arguments);

// Dynamic method call on an object
$object->{<method_name>}(arguments);
$object::{<method_name>}(arguments);

// Dynamic method call on a dynamically-generated object
(<object>)->{<method_name>}(arguments);
(<object>)::{<method_name>}(arguments);

// Dynamic method call, statically
ClassName::{<method_name>}(arguments);
(<class_name>)::{<method_name>}(arguments);

// Dynamic method call, array-like (no different between static
// and non-static calls
[<object>, <method_name>](arguments);

// Dynamic method call, array-like, statically
[<class_name>, <method_name>](arguments);

Special thanks to this PHP talk.

給妳壹絲溫柔 2024-07-31 10:30:54

是的,这是可能的:

function foo($msg) {
    echo $msg."<br />";
}
$var1 = "foo";
$var1("testing 1,2,3");

来源:http ://www.onlamp.com/pub/a/php/2001/05/17/php_foundations.html?page=2

Yes, it is possible:

function foo($msg) {
    echo $msg."<br />";
}
$var1 = "foo";
$var1("testing 1,2,3");

Source: http://www.onlamp.com/pub/a/php/2001/05/17/php_foundations.html?page=2

长安忆 2024-07-31 10:30:54

如前所述,有几种方法可以实现此目的,其中最安全的方法可能是 call_user_func(),或者如果必须的话,您也可以沿着 $function_name() 的路线进行>。 可以使用这两种方法传递参数,因此

$function_name = 'foobar';

$function_name(arg1, arg2);

call_user_func_array($function_name, array(arg1, arg2));

如果您调用的函数属于一个对象,您仍然可以使用其中任何一个

$object->$function_name(arg1, arg2);

call_user_func_array(array($object, $function_name), array(arg1, arg2));

但是如果您要使用 $function_name() 方法如果名称是动态的,则测试该函数是否存在可能是个好主意

if(method_exists($object, $function_name))
{
    $object->$function_name(arg1, arg2);
}

As already mentioned, there are a few ways to achieve this with possibly the safest method being call_user_func() or if you must you can also go down the route of $function_name(). It is possible to pass arguments using both of these methods as so

$function_name = 'foobar';

$function_name(arg1, arg2);

call_user_func_array($function_name, array(arg1, arg2));

If the function you are calling belongs to an object you can still use either of these

$object->$function_name(arg1, arg2);

call_user_func_array(array($object, $function_name), array(arg1, arg2));

However if you are going to use the $function_name() method it may be a good idea to test for the existence of the function if the name is in any way dynamic

if(method_exists($object, $function_name))
{
    $object->$function_name(arg1, arg2);
}
蘑菇王子 2024-07-31 10:30:54

晚了几年,但恕我直言,这是现在最好的方式:

$x = (new ReflectionFunction("foo"))->getClosure();
$x();

A few years late, but this is the best manner now imho:

$x = (new ReflectionFunction("foo"))->getClosure();
$x();
不语却知心 2024-07-31 10:30:54

如果其他人被谷歌带到这里,因为他们试图在类中使用变量作为方法,下面是一个实际有效的代码示例。 以上都不适合我的情况。 主要区别在于 $c = & 声明中的 & new...&$ccall_user_func 中传递。

我的具体情况是,当实现某人与颜色有关的代码以及 csscolor.php 类中的两个成员方法 lighten()darken() 时。 无论出于何种原因,我希望相同的代码能够调用变亮或变暗,而不是通过逻辑选择它。 这可能是由于我固执地不只使用 if-else 或更改调用此方法的代码的结果。

$lightdark="lighten"; // or optionally can be darken
$color="fcc";   // a hex color
$percent=0.15;  
include_once("csscolor.php");
$c = & new CSS_Color($color);
$rtn=call_user_func( array(&$c,$lightdark),$color,$percent);

请注意,尝试使用 $c->{...} 进行任何操作均无效。 在仔细阅读了 call_user_func php.net 页面底部的读者提供的内容后,我能够将上述内容拼凑起来。 另请注意,$params 作为数组对我不起作用

// This doesn't work:
$params=Array($color,$percent);
$rtn=call_user_func( array(&$c,$lightdark),$params);

上述尝试将给出有关需要第二个参数(百分比)的方法的警告。

In case someone else is brought here by google because they were trying to use a variable for a method within a class, the below is a code sample which will actually work. None of the above worked for my situation. The key difference is the & in the declaration of $c = & new... and &$c being passed in call_user_func.

My specific case is when implementing someone's code having to do with colors and two member methods lighten() and darken() from the csscolor.php class. For whatever reason, I wanted to have the same code be able to call lighten or darken rather than select it out with logic. This may be the result of my stubbornness to not just use if-else or to change the code calling this method.

$lightdark="lighten"; // or optionally can be darken
$color="fcc";   // a hex color
$percent=0.15;  
include_once("csscolor.php");
$c = & new CSS_Color($color);
$rtn=call_user_func( array(&$c,$lightdark),$color,$percent);

Note that trying anything with $c->{...} didn't work. Upon perusing the reader-contributed content at the bottom of php.net's page on call_user_func, I was able to piece together the above. Also, note that $params as an array didn't work for me:

// This doesn't work:
$params=Array($color,$percent);
$rtn=call_user_func( array(&$c,$lightdark),$params);

This above attempt would give a warning about the method expecting a 2nd argument (percent).

你好,陌生人 2024-07-31 10:30:54

为了完整起见,您还可以使用 eval()

$functionName = "foo()";
eval($functionName);

但是,call_user_func() 是正确的方法。

For the sake of completeness, you can also use eval():

$functionName = "foo()";
eval($functionName);

However, call_user_func() is the proper way.

樱花落人离去 2024-07-31 10:30:54

动态函数名称和命名空间

只是在使用命名空间时添加有关动态函数名称的一点。

如果您使用命名空间,除非您的函数位于全局命名空间中,否则以下操作将不起作用

namespace greetings;

function hello()
{
    // do something
}

$myvar = "hello";
$myvar(); // interpreted as "\hello();"

该怎么办?

您必须使用 call_user_func() 来代替:

// if hello() is in the current namespace
call_user_func(__NAMESPACE__.'\\'.$myvar);

// if hello() is in another namespace
call_user_func('mynamespace\\'.$myvar);

Dynamic function names and namespaces

Just to add a point about dynamic function names when using namespaces.

If you're using namespaces, the following won't work except if your function is in the global namespace:

namespace greetings;

function hello()
{
    // do something
}

$myvar = "hello";
$myvar(); // interpreted as "\hello();"

What to do?

You have to use call_user_func() instead:

// if hello() is in the current namespace
call_user_func(__NAMESPACE__.'\\'.$myvar);

// if hello() is in another namespace
call_user_func('mynamespace\\'.$myvar);
风渺 2024-07-31 10:30:54

补充@Chris K的答案,如果你想调用一个对象的方法,你可以在闭包的帮助下使用单个变量来调用它:

function get_method($object, $method){
    return function() use($object, $method){
        $args = func_get_args();
        return call_user_func_array(array($object, $method), $args);           
    };
}

class test{        

    function echo_this($text){
        echo $text;
    }
}

$test = new test();
$echo = get_method($test, 'echo_this');
$echo('Hello');  //Output is "Hello"

我发布了另一个例子这里

Complementing the answer of @Chris K if you want to call an object's method, you can call it using a single variable with the help of a closure:

function get_method($object, $method){
    return function() use($object, $method){
        $args = func_get_args();
        return call_user_func_array(array($object, $method), $args);           
    };
}

class test{        

    function echo_this($text){
        echo $text;
    }
}

$test = new test();
$echo = get_method($test, 'echo_this');
$echo('Hello');  //Output is "Hello"

I posted another example here

审判长 2024-07-31 10:30:54

使用 call_user_func 函数。

Use the call_user_func function.

不美如何 2024-07-31 10:30:54

我从这个问题和答案中学到了什么。 谢谢大家!

假设我有这些变量和函数:

$functionName1 = "sayHello";
$functionName2 = "sayHelloTo";
$functionName3 = "saySomethingTo";

$friend = "John";
$datas = array(
    "something"=>"how are you?",
    "to"=>"Sarah"
);

function sayHello()
{
echo "Hello!";
}

function sayHelloTo($to)
{
echo "Dear $to, hello!";
}

function saySomethingTo($something, $to)
{
echo "Dear $to, $something";
}
  1. 调用函数不带参数

    // 调用 sayHello() 
      call_user_func($functionName1);  
      

    <块引用>

    你好!

  2. 使用 1 个参数调用函数

    // 调用 sayHelloTo("John") 
      call_user_func($functionName2, $friend); 
      

    <块引用>

    亲爱的约翰,你好!

  3. 使用 1 个或多个参数调用函数
    如果您动态调用函数并且每个函数具有不同数量的参数,这将很有用。 这是我一直在寻找(并解决)的案例。 call_user_func_array 是关键

    // 您可以添加您的参数 
      // 1. 通过硬编码静态地,  
      $arguments[0] = "你好吗?";   // 我的$东西 
      $arguments[1] = "莎拉";   // 我的 $to 
    
      // 2. OR 动态使用 foreach 
      $参数= NULL; 
      foreach($数据作为$数据)  
      { 
          $参数[] = $数据; 
      } 
    
      // 调用 saySomethingTo("你好吗?", "莎拉") 
      call_user_func_array($functionName3, $arguments); 
      

    <块引用>

    亲爱的莎拉,你好吗?

耶再见!

What I learnt from this question and the answers. Thanks all!

Let say I have these variables and functions:

$functionName1 = "sayHello";
$functionName2 = "sayHelloTo";
$functionName3 = "saySomethingTo";

$friend = "John";
$datas = array(
    "something"=>"how are you?",
    "to"=>"Sarah"
);

function sayHello()
{
echo "Hello!";
}

function sayHelloTo($to)
{
echo "Dear $to, hello!";
}

function saySomethingTo($something, $to)
{
echo "Dear $to, $something";
}
  1. To call function without arguments

    // Calling sayHello()
    call_user_func($functionName1); 
    

    Hello!

  2. To call function with 1 argument

    // Calling sayHelloTo("John")
    call_user_func($functionName2, $friend);
    

    Dear John, hello!

  3. To call function with 1 or more arguments
    This will be useful if you are dynamically calling your functions and each function have different number of arguments. This is my case that I have been looking for (and solved). call_user_func_array is the key

    // You can add your arguments
    // 1. statically by hard-code, 
    $arguments[0] = "how are you?"; // my $something
    $arguments[1] = "Sarah"; // my $to
    
    // 2. OR dynamically using foreach
    $arguments = NULL;
    foreach($datas as $data) 
    {
        $arguments[] = $data;
    }
    
    // Calling saySomethingTo("how are you?", "Sarah")
    call_user_func_array($functionName3, $arguments);
    

    Dear Sarah, how are you?

Yay bye!

故人爱我别走 2024-07-31 10:30:54

如果您在对象上下文中尝试动态调用函数,请尝试如下代码:

$this->{$variable}();

If you were in a object context trying to call a function dynamically please try something like this code bellow:

$this->{$variable}();
旧街凉风 2024-07-31 10:30:54

使用存储在变量中的名称安全地调用函数的最简单方法是,

//I want to call method deploy that is stored in functionname 
$functionname = 'deploy';

$retVal = {$functionname}('parameters');

我使用如下方式在 Laravel 中动态创建迁移表,

foreach(App\Test::$columns as $name => $column){
        $table->{$column[0]}($name);
}

The easiest way to call a function safely using the name stored in a variable is,

//I want to call method deploy that is stored in functionname 
$functionname = 'deploy';

$retVal = {$functionname}('parameters');

I have used like below to create migration tables in Laravel dynamically,

foreach(App\Test::$columns as $name => $column){
        $table->{$column[0]}($name);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文