Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (34 sloc) 1.72 KB
title subTitle currentMenu
Command line and scripts
PHP Best practices

Use a "Console" component

Intermediate ###Rule

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:



// 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.

Why? Because:

  • 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.