0

Possible Duplicate:
Can I override the PHP built-in function echo()?

In languages such as Python, "echo" is not a language construct. Is it possible to shadow the built-in "echo" construct in PHP and implement a function like this:

function echo($value) {
    return $value;
}

Here is the error message:

PHP Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting identifier (T_STRING) or '('
Community
  • 1
  • 1
Nathan Farrington
  • 1,890
  • 1
  • 17
  • 27
  • 3
    [http://stackoverflow.com/questions/2182743/can-i-override-the-php-built-in-function-echo](http://stackoverflow.com/questions/2182743/can-i-override-the-php-built-in-function-echo) – Sara Sep 27 '12 at 00:47
  • no. You cannot. Simply. You could create _echo though. – Gavin Sep 27 '12 at 00:49
  • well there you go, the answer is no. keep in mind that you can use `echo` like a function though, i.e., `echo('cheese')`. if you need a return value, `print('x')` will return `1`. – mpen Sep 27 '12 at 00:49
  • people usually just create wrapper functions for this sort of thing. what do you plan to us it for and have you already considered a wrapper function? – Flosculus Sep 27 '12 at 00:51
  • I'm testing a cross-language RPC library and want to echo things back to the caller. A Python server can implement echo but a PHP server cannot. Hence, need to choose a different function name that is compatible everywhere. – Nathan Farrington Sep 27 '12 at 00:53
  • If you implement some variant of RPC, then just use a function name map. You're not supposed to accept *any* incoming function name plainly anyway. Also [`rename_function()`](http://php.net/manual/en/function.rename-function.php). – mario Sep 27 '12 at 01:06
  • now why would you want to do that really? – Ibu Sep 27 '12 at 01:07
  • in your system, how is python interfacing with php? – Flosculus Sep 27 '12 at 01:13
  • Why do you need to use the same name in PHP as you use in Python? – Barmar Sep 27 '12 at 01:25
  • This is my RPC library: https://github.com/nfarring/redisrpc. Either the client or server can be written in PHP, Python, or Ruby, and hopefully other languages if I can find the time. But if PHP doesn't allow a function called "echo", then a client written in a different language will be incompatible with a PHP server. I guess the same problem would happen in Python if I wanted to make a function called "pass". Not a huge deal really. – Nathan Farrington Sep 27 '12 at 03:31

0 Answers0