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()
$this->say($whatever)