0

If you run php -a on the cmd it will become the php's interactive shell. How can I do it from a php script? For example, I can run a php script with the line exec('php -S localhost:8080'); in it to start a bult-in server and it will block the current cmd for the logs of the server. That's exactly what I want but for php -a, to have the current cmd become the php's interactive shell but using the exec() function does not seem to work for this.

Anyone knows how to do it? In the end it should look just a little like laravel's tinker, but not that fancy. Just want to require some files and have access to their data from the CLI.

index.php:

require 'variables.php';

exec('php -a');

then on the cmd: php index.php (similar to laravel's artisan) and it would turn into the interactive shell so I could do something like so:

Interactive Shell

php > echo $variable_on_variables_file;
value_of_variable // outputs value of variable here on the terminal
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • 1
    For what reason? I'm really interesting – lezhni May 23 '23 at 20:31
  • 1
    `exec('php -S localhost:8080');` - So from one PHP script you want to launch another process that starts the PHP local development server? How are you accessing the first PHP script? Through a browser? If so, the answer is HTML/JS/CSS/HTTP. If through the CLI, then why two scripts? I see this is also tagged Laravel Tinker, and I'll confess I'm ignorant to that, so maybe it makes more sense to someone familiar with that. – Chris Haas May 23 '23 at 21:20
  • 1
    If you're trying to pipe web user typing into an interactive PHP instance that sounds very dangerous. Perhaps you could edit your question to describe the problem you're trying to solve with this? – Dave S May 23 '23 at 21:38
  • To clarify, I'm trying to access it from the CLI. I'll trigger a php script to require another php file containing some variables and then start the interactive shell to access those varibles. It's just for a small side project and mainly for learning purposes, a proof of concept. I'll edit the question to clarify further. – Bruno Gomes May 23 '23 at 22:03
  • 1
    Would it work to just start the shell directly with files? https://stackoverflow.com/questions/26473168/php-interactive-load-file-from-command-line or https://stackoverflow.com/questions/18294683/initialising-php-interactive or https://stackoverflow.com/questions/57921159/autoloading-classes-into-php-interactive-shell – Dave S May 23 '23 at 22:10
  • @DaveS Not quite because I need it to be started from the php file. And the code works without any errors, the problem is that it does not transform the terminal where it has been called into the interactive one and that's exaclty what I need, just like laravel's tinker seems to do. – Bruno Gomes May 23 '23 at 22:14
  • 1
    Tinker runs through PsySH if that helps: https://github.com/bobthecow/psysh – Chris Haas May 23 '23 at 22:35
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 24 '23 at 01:52

1 Answers1

-1

Thank you all for the comments. Turns out the proper term for what I want is "read-evaluate-print loop" (repl) and, at the end, I've gone with exactly a loop that keeps on reading the user input and evaluating and printing the result, like an repl is supposed to do haha. It works well for me, thanks again!