4


we can simply override a default php function by using the following code :

namespace blarg;
function time() {
  echo "test !";
}
time();

but ! is it possible to override the "eval" functon ?

namespace blarg;
function eval() {
echo "test !";
}
eval();

?

thanks in advance

According to the answeres :

  1. eval() is a language construct and not a function, we can't override it
    2.Im not really overriding the time() function in my example. I'm just creating a blarg\time() function

I understood
BUT
Actually i'm writtin sumting like a debugger and i need to change the default behaviour of php functons or some language construct (e.g eval) Is there anyway to do this ? Is it possible to change the php source and compile it ? (Is there any specific file to edit ?)

Shahrokhian
  • 1,100
  • 13
  • 28
  • 3
    **eval()** is a language construct and not a function, you can't override it. – Karolis Aug 03 '11 at 19:36
  • 1
    And you're not really overriding the time() function in your example. You're just creating a `blarg\time()` function. – Mchl Aug 03 '11 at 19:41
  • Here is the similar SO question - http://stackoverflow.com/questions/2326835/redefine-built-in-php-functions – Rakesh Sankar Aug 04 '11 at 06:13

2 Answers2

3

Note: Because this is a language construct and not a function, it cannot be called using variable functions Manual

So, it can't be overriden as echo/php/include too

RiaD
  • 46,822
  • 11
  • 79
  • 123
  • 2
    This, +1. Note about the `include` portion however, you can of course override the `file://` protocol with a stream wrapper of your own... – Wrikken Aug 03 '11 at 19:42
2
namespace blarg;
function time() {
  echo "test !";
}
time();

This does not override the builtin time() function. What this does is create the function blarg\time() -- that is, both time() and blarg\time() exist and can be called, so never was time() overridden.

When you call time() in the blarg namespace, both the builtin time() and local blarg\time() functions are candidates to be called; however, PHP will disambiguate by always calling the most local name, which in this case is blarg\time().

Now, if we ask if it is possible to introduce a function locally named eval(), the answer is no, because eval is a reserved word in the PHP language because of its special meaning -- referred to as a language construct. Other examples of the same nature are isset, empty, include, and require.

erisco
  • 14,154
  • 2
  • 40
  • 45