【问题标题】:Error: Call to undefined function in Command line code on line 1错误:在第 1 行的命令行代码中调用未定义函数
【发布时间】:2018-05-16 10:20:37
【问题描述】:

我正在使用我的命令行来执行类中的方法。但是我不断收到以下消息:

Error: Call to undefined function test() in Command line code on line 1

这是我的类和方法:

class DatasetTest
{
   public function test()
    {
       echo ""worked";
   }

}

这是我的命令行命令:

$ php -r 'require "DatasetTest.php"; test();'

我也试过了:

$ php -r 'require "DatasetTest.php"; $this->test();'

但收到以下消息;

Error: Using $this when not in object context in Command line code on line 1

【问题讨论】:

标签: php command-line


【解决方案1】:

你不能在不实例化对象的情况下执行方法,除非你将它定义为静态的。

解决方案 #1

class DatasetTest
{
   public static function test()
   {
       echo "worked";
   }

}

还有一个错误,我用双引号更正了。然后就可以执行了

$ php -r 'require "DatasetTest.php"; DatasetTest::test();'

解决方案 #2

function test()
{
    echo "worked";
}

然后

$ php -r 'require "DatasetTest.php"; test();'

【讨论】:

    猜你喜欢
    • 2016-09-29
    • 2012-04-21
    • 2011-04-19
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2017-09-03
    • 2011-07-07
    相关资源
    最近更新 更多