Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
title subTitle currentMenu
Command line and scripts
PHP Best practices
php-cli

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.

###Explanation

What you should NOT do:

You should avoid accessing scripts by running them directly from the PHP CLI:

my_script.php

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

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: