如何从命令行在类中执行方法

时间:2020-12-07 22:22:36

Basically I have a PHP class that that I want to test from the commandline and run a certain method. I am sure this is a basic question, but I am missing something from the docs. I know how to run a file, obviously php -f but not sure how to run that file which is a class and execute a given method

基本上我有一个PHP类,我想从命令行测试并运行某种方法。我确信这是一个基本问题,但我遗漏了文档中的内容。我知道如何运行文件,显然是php -f但不知道如何运行该文件是一个类并执行给定的方法

4 个解决方案

#1


35  

This will work:

这将有效:

php -r 'include "MyClass.php"; MyClass::foo();'

But I don't see any reasons do to that besides testing though.

但是除了测试之外我没有看到任何理由。

#2


12  

I would probably use call_user_func to avoid harcoding class or method names. Input should probably use some kinf of validation, though...

我可能会使用call_user_func来避免编码类或方法名称。输入应该使用一些验证,但......

<?php

class MyClass
{
    public function Sum($a, $b)
    {
        $sum = $a+$b;
        echo "Sum($a, $b) = $sum";
    }
}


// position [0] is the script's file name
array_shift(&$argv);
$className = array_shift(&$argv);
$funcName = array_shift(&$argv);

echo "Calling '$className::$funcName'...\n";

call_user_func_array(array($className, $funcName), $argv);

?>

Result:

结果:

E:\>php testClass.php MyClass Sum 2 3
Calling 'MyClass::Sum'...
Sum(2, 3) = 5

#3


7  

Here's a neater example of Repox's code. This will only run de method when called from the commandline.

这是Repox代码的一个更简洁的例子。这只会在从命令行调用时运行de方法。

<?php
class MyClass
{
  public function hello()
  {
    return "world";
  }
}

// Only run this when executed on the commandline
if (php_sapi_name() == 'cli') {
    $obj = new MyClass();
    echo $obj->hello();
}

?>

#4


4  

As Pekka already mentioned, you need to write a script that handles the execution of the specific method and then run it from your commandline.

正如Pekka已经提到的,您需要编写一个脚本来处理特定方法的执行,然后从命令行运行它。

test.php:

test.php的:

<?php
class MyClass
{
  public function hello()
  {
    return "world";
  }
}

$obj = new MyClass();
echo $obj->hello();
?>

And in your commandline

并在您的命令行中

php -f test.php

#1


35  

This will work:

这将有效:

php -r 'include "MyClass.php"; MyClass::foo();'

But I don't see any reasons do to that besides testing though.

但是除了测试之外我没有看到任何理由。

#2


12  

I would probably use call_user_func to avoid harcoding class or method names. Input should probably use some kinf of validation, though...

我可能会使用call_user_func来避免编码类或方法名称。输入应该使用一些验证,但......

<?php

class MyClass
{
    public function Sum($a, $b)
    {
        $sum = $a+$b;
        echo "Sum($a, $b) = $sum";
    }
}


// position [0] is the script's file name
array_shift(&$argv);
$className = array_shift(&$argv);
$funcName = array_shift(&$argv);

echo "Calling '$className::$funcName'...\n";

call_user_func_array(array($className, $funcName), $argv);

?>

Result:

结果:

E:\>php testClass.php MyClass Sum 2 3
Calling 'MyClass::Sum'...
Sum(2, 3) = 5

#3


7  

Here's a neater example of Repox's code. This will only run de method when called from the commandline.

这是Repox代码的一个更简洁的例子。这只会在从命令行调用时运行de方法。

<?php
class MyClass
{
  public function hello()
  {
    return "world";
  }
}

// Only run this when executed on the commandline
if (php_sapi_name() == 'cli') {
    $obj = new MyClass();
    echo $obj->hello();
}

?>

#4


4  

As Pekka already mentioned, you need to write a script that handles the execution of the specific method and then run it from your commandline.

正如Pekka已经提到的,您需要编写一个脚本来处理特定方法的执行,然后从命令行运行它。

test.php:

test.php的:

<?php
class MyClass
{
  public function hello()
  {
    return "world";
  }
}

$obj = new MyClass();
echo $obj->hello();
?>

And in your commandline

并在您的命令行中

php -f test.php