0

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()

14
  • what IS happening? Commented May 4, 2018 at 15:01
  • somehow your function say is executing? Commented May 4, 2018 at 15:04
  • @coder I edited my code to be more clear Commented May 4, 2018 at 15:06
  • its working then. but it should be $this->say($whatever) Commented May 4, 2018 at 15:07
  • 1
    you don't define $str. did you mean $line? Commented May 4, 2018 at 15:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It outputs a "e" whatever I do, weird
say "something" doesn't do anything it just displays the say function without any string and just blank space
just typing say displays an e
OK - can't replicate that. I'm assuming you've made no changes to any of the other code in your original question.
Also - worth adding the lines I've added to the answer - they show any errors in your code when testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.