是否可以从数组执行命令?

刘易斯4u

出于好奇,是否有可能做这样的事情:

$commands = [
    strtolower('JUST TESTING'),
    date('Y-m-d H:i:s'),
    strtoupper('Done!'),
];


foreach ($commands as $command) {
    $command;
}

这当然行不通!有没有办法让它工作?


我的具体用例是这样的:

private function dropDatabasesAndMySQLUsers(): void
{
    foreach ($this->getCommands() as $command) {
        $command;
    }
    $this->info('Done! All app Databases and MySQL users are dropped');
}

public function getCommands(): array
{
    return [
        \DB::statement("DROP USER IF EXISTS 'myuser'@'localhost'"), 
        \DB::statement("DROP DATABASE IF EXISTS manager")
        // I have about 20-30 of these
    ];
}
昆汀

以可重用且可传递的方式存储“命令”的标准方法是使用函数

<?php

$commands = [
     function () { print 1+2; },
     function () { print 2+6; }
];

foreach ($commands as $command) {
    $command();
    print "\n";
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章