The Artima Developer Community
Sponsored Link

PHP Buzz Forum
PHP Command Line rocks

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Pure-PHP

Posts: 20
Nickname: purephp
Registered: Mar, 2005

Pure-PHP is kind of Blog about PHP, and unconventional methodes to boost PHP-apps
PHP Command Line rocks Posted: Mar 18, 2005 11:16 PM
Reply to this message Reply

This post originated from an RSS feed registered with PHP Buzz by Pure-PHP.
Original Post: PHP Command Line rocks
Feed Title: Pure PHP - PHP unconventional
Feed URL: http://www.iran-fun.com/
Feed Description: Unconventional methodes to boost php apps
Latest PHP Buzz Posts
Latest PHP Buzz Posts by Pure-PHP
Latest Posts From Pure PHP - PHP unconventional

Advertisement

Recently I wrote a PHP Command line app, I was amazed, how easy it was, to write command line app in PHP, and it really rocks.

Here is very simple Command Line handler class, it may be usefull for your apps.

<?php

class CliHandler{
    
private $in;
    
private $out;
    
private $handler;
    
public function CliHandler($handler){
        
$this->in = fopen("php://stdin","r");
        
$this->out = fopen("php://stdout", "w");
        if(
is_object($handler)){
            
$this->handler = $handler;
        }
    }    
    
public function run(){
        
$str = "Try these commands:\n"
        
.implode("\n",get_class_methods($this->handler));
        
$this->out($str);
        while(
$line = rtrim(fgets($this->in, 1024))){
            if(
method_exists($this->handler,$line)){
                
$out = $this->handler->$line();
                if(
$out){
                    
$this->out($out);
                }
            }
        }
    }
    
public function out($str){
        
fwrite($this->out,$str."\n");
    }
}
class
AnyClass{
    
public function start(){
        return
"started";
    }
    
public function stop(){
        return
"stoppded";
    }
}
$cli = new CliHandler(new AnyClass());
$cli->run();
?>

CliHandler accepts any class als argument.
Try this.

/usr/local/php/PHP5 CliHandler.class.php
output: Try these command:
start
stop
enter "start"
output: started

Read: PHP Command Line rocks

Topic: Map of topics on Simple Thoughts Blog Previous Topic   Next Topic Topic: require_once, one optimization too many?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use