简体   繁体   中英

How to run a php static function from the command line?

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?

create a file run.php :

<?php
require 'thefile.php';
MyModel::myMethod();

虽然我不建议这样做,但您可以这样称呼它:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM