Command line and scripts
PHP Best practices
Use a "Console" component
Need to run a PHP script from the command line? Do not execute your PHP files directly. Instead, consider using a "Console" component.
What you should NOT do:
You should avoid accessing scripts by running them directly from the PHP CLI:
#!/usr/bin/php <?php // Your PHP script that you run by typing "php my_script.php"
Most developers know they should use a MVC framework with a router component instead of directly accessing PHP files from the web. When working with scripts triggered from the command line (or from a CRON task), the same rule applies!
So instead, rather than directly accessing your scripts using
php my_script.php, you should consider using a console component from one of the major frameworks out there.
- You will write object oriented scripts
- You will get a nice interface to access the command line, including utility functions to:
- manage options
- automatically document your script
- colorize your output
Here are a few console components out there: