tags:

views:

963

answers:

4

I have a function that is effectively a replacement for print, and I want to call it without parentheses, just like calling print.

# Replace
print $foo, $bar, "\n";

# with
myprint $foo, $bar, "\n";

In Perl, you can create subroutines with parameter templates and it allows exactly this behavior if you define a subroutine as

sub myprint(@) { ... }

Anything similar in PHP?

+7  A: 

No, you can't do that in PHP. Print isn't actually a function, it's a "language construct".

Greg
+12  A: 

print is not a variable functions

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

And :

Variable functions

PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.

Daok
+5  A: 

Only by editing the PHP codebase and adding a new language construct.

Adam Davis
A: 

Nope, PHP won't allow you to do that.

Vestonian