Command line and scripts PHP Best practices
Use a "Console" component
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!
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:
At TheCodingMachine, we use Mouf's console which is essentially a wrapper around the very nice Symfony console that enables using dependency injection into commands.
Found a typo? Something is wrong in this documentation? Just fork and edit it!