PHP Init Style Status Message

|

Yesterday I talked about created SUCCESS and FAILED status messages in a Bash script (Bash Script Init Style Status Message). Well, on occasion I use PHP for shell/system scripting and of course I thought it would be nice to incorporate my new status messages in those scripts as well. Turns out that it was quite easy to port over.

The Functions

You can simply include the following functions in your PHP script/class.

  private function moveToStatusCol() { echo "\033[60G"; }
  private function setColorSuccess() { echo "\033[1;32m"; }
  private function setColorFailure() { echo "\033[1;31m"; }
  private function setColorNormal()  { echo "\033[0;39m"; }

  private function echo_success()
  {
    $this->moveToStatusCol();
    print '[';
    $this->setColorSuccess();
    print '  OK  ';
    $this->setColorNormal();
    print "]\n";
    $this->log("OK");
  }
  private function echo_failure()
  {
    $this->moveToStatusCol();
    print '[';
    $this->setColorFailure();
    print 'FAILED';
    $this->setColorNormal();
    print "]\n";
    $this->log("FAILED");
  }

If you read my previous article then you can readily see that the code is almost verbatim, it just uses PHP syntax instead of Bash.

Usage

The above functions were taken out of a class that I was using. If you wanted to include these functions in your class then you could simply call them as follows:

public function test_some_stuff()
{
  // Return Value
  $bRetVal = FALSE;
  try
  {
    // Test some stuff here that might throw an exception
    // ...

    // If we get this far then we passed!
    $this->echo_success();
    $bRetVal = TRUE;
  }
  catch(Exception $e)
  {
    $this->echo_failure();
    $this->log("ERROR : {$e->getMessage()}\n");
  }
  // Return Value
  return $bRetVal;
}

Well, I hope you can find some use for these functions and thank you so much for stopping by! Please leave comments below (if you have any).