2

I want to run a function in a php script from the linux command line. The php script looks as follows:

<?php
namespace mycompany\admin;
class MyModel
{
    public static function myMethod() {
        echo 'something';
    }
}

Normally I would do something like this: php thefile.php, but since the function is not called anywhere, it isn't run. I have no idea however, how I could call that function from the command line.

Anybody?

1

3 Answers 3

5

create a file run.php:

<?php
require 'thefile.php';
MyModel::myMethod();
Sign up to request clarification or add additional context in comments.

Comments

5

While I would not recommend it, you could call it like this:

php -r "include('thefile.php');mycompany\admin\MyModel::MyMethod();"

Comments

3

You cannot run a PHP method from command line. You need to call it from a script. You could do this:

<?php
namespace mycompany\admin;
class MyModel
{
    public static function myMethod() {
        echo 'something';
    }
}
MyModel::myMethod();

and run it from CLI like so: php -r myscript.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.