-1

i am using my command line to excute a method within a class. however i keep getting the following message:

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

this is my class and method:

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

}

this is my command line order:

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

i also tried:

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

but got following message;

Error: Using $this when not in object context in Command line code on line 1
Paul Kendal
  • 559
  • 9
  • 24

1 Answers1

1

You cannot execute a method without instantiate the object, unless you define it as static.

Solution #1

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

}

There is also a mistake that i've corrected with double quotes. Then you can execute

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

Solution #2

function test()
{
    echo "worked";
}

Then

$ php -r 'require "DatasetTest.php"; test();'
Jack Skeletron
  • 1,351
  • 13
  • 37