Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to require a .php file via command line parameter before running a php script?

Situation

I'm running linux with a bash 4 compatible shell.

I have two files:

A.class.php:

<?php

class A{

    public static function foo(){
        echo "foo\n";
    }
}

A.php:

<?php

A:foo();

Question

Is it possible to require A.class.php before running A.php from the command line without editing the files?

Something like:

php --require "A.class.php" A.php 

Things I tried

I tried to concatenate <?php require 'A.class.php' ?> with the contents of the file A.php and then pipe it to php like:

echo "<?php require 'A.class.php'?>$(<A.php)" | php

which works quite well but is kind of hackish and a lot to write. Maybe there is some easier way to do this?

like image 216
aichingm Avatar asked Aug 30 '25 16:08

aichingm


1 Answers

You could use a -d directive=value to set the auto_prepend_file directive.

php -d auto_prepend_file=A.class.php -f A.php

Or instead of passing script file names you could pass php code that loads the scripts directly as a parameter:

php -r "require 'A.class.php'; require 'A.php';"
like image 76
VolkerK Avatar answered Sep 02 '25 04:09

VolkerK