Margaret Osborn
Margaret Osborn

Reputation: 39

php - CLI and custom commands

I'm currently trying to make a php script that can be interactive with the user by executing commands. The user have to use the script in the Terminal.

here is an example about what I'm talking about:

>php robot.php
>Hello, I'm the GoodRobot number 8731039139, nice to meet you!
>Please type a command below:
>say "Hello" **// user types this**
>GoodRobot no 8731039139: Hello **// robot.php outputs this**

I want it to make it interactive as soon as init() is called but I've been trying to do what you can see below and it doesn't seem to work.

Here is my full code

<?php

class Robot 
{
    // ?? \\
}

class GoodRobot extends Robot
{
    static protected $serialNumber; 

    private $name;
    private $type = "Droid";

    public function __construct($name)
    {
        $this->name = $name;
        echo "Hello, I'm the GoodRobot number " . self::$serialNumber . ", nice to meet you!\n";
    }

    public function init()
    {
        echo "Please type a command below:" . PHP_EOL;
        $handle = fopen ("php://stdin","r");
        $line = fgets($handle);
        if(trim($line) != 'say')
        {
            print(say($str));
        }
        elseif (trim($line) != 'help') {
            print(help());
        }
        else
        {
            echo "ERROR: Unknown command. Type \"help\" if you need help." . PHP_EOL;
        }
    }

    public function setName($name) 
    {
        $this->name = $name;
    }

    public function getName($name)
    {
        return $this->name;
    }

    public function help()
    {
        echo "Here is a list of commands: \n - say \"something\" \n - shutDown \n";
    }

    public function say($str)
    {
        echo "GoodRobot no " . self::$serialNumber . " : " . $str . "\n";
    }

    public function turnOn()
    {
        echo "Waking up... Please be patient.\n";
        parent::__construct();
    }

    public function shutDown()
    {
        echo "Shutting down...\n";
        exit;
    }
}

$hi = new GoodRobot("hi");
$hi -> init();

When I'm trying to "say"

It display a fatal error

PHP Fatal error: Uncaught Error: Call to undefined function say()

Upvotes: 0

Views: 72

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57131

You need to use $this-> when referring to a method in the current class. I've also changed your input to just readline() as this handles the IO.

Also note the string manipulation to change the say "Hello" to remove the first bit and any surrounding quotes.

public function init()
{
    echo "Please type a command below:" . PHP_EOL;
    $line = readline();
    if(substr($line, 0, 3) == 'say')
    {
        $line = substr($line, 3);
        $line = rtrim(trim($line,"\" "),"\" \n\r");
        $this->say($line);
    }
    elseif (substr($line, 0, 4) == 'help') {
        $this->help();
    }
    else
    {
        echo "ERROR: Unknown command. Type \"help\" if you need help." . PHP_EOL;
    }
}

I'm not sure why you had print() around the say() call, say() echos out the string, so the print is not needed.

You also need to check the first 3 chars are 'say' rather than using trim(). Same for 'help' (although 4 chars this time).

Update: When testing add..

ini_set('display_errors', 'On');
error_reporting(E_ALL);

to the top of your script, it helps in showing any errors in the code.

Upvotes: 1

Related Questions