Hello everyone,
I've recently came across an odd situation where FUNCTION can
produce a misleading results when using traits with aliased methods, see
https://bugs.php.net/bug.php?id=61033.
https://bugs.php.net/bug.php?id=61033
This has been closed and I've been asked start a discussion on here if I
disagree with the ruling. The reason why I feel this should be changes
to reflect the actual called function name is that one of main uses of
the FUNCTION constant it to refer back to the function that is
currently running, for the use recursive functions:
trait FooTrait {
public function foo($n) {
if ($n == 0) return;
echo $n . PHP_EOL;
$function = FUNCTION;
$this->$function(--$n);
}
}
class MyClass {
use FooTrait { foo as public countdown; }
public function foo() {
echo 'MyClass::foo()';
}
}
$instance = new MyClass();
$instance->countdown(3);
This example will produce running (PHP 5.4 RC7):
10
MyClass::foo()
Which as you can see is not the desired effect.
I send this email in the hope this can get the discussion under way
regarding the magic constant FUNCTION.
Thanks,
Marc Easen
Hi Marc:
The reason why I feel this should be changes to reflect the actual called function name is that one of main uses of the FUNCTION constant it to refer back to the function that is currently running, for the use recursive functions:
I still maintain the position that FUNCTION, as LINE and FILE, is supposed to be a lexical constant. (In retro-spective, adapting CLASS to do even more magic might have been a mistake, from a conceptual point of view.)
So far, I was refraining from proposing any additions to the RFC before 5.4 is finally out and we see how it is used in the wild.
But since recursive function calls are indeed a problem, we should look for a better solution.
I think, self
is already a keyword anyway, perhaps we can use that for self-referential/recursive function calls in traits?
Best regards
Stefan
--
Stefan Marr
Software Languages Lab
Vrije Universiteit Brussel
Pleinlaan 2 / B-1050 Brussels / Belgium
http://soft.vub.ac.be/~smarr
Phone: +32 2 629 2974
Fax: +32 2 629 3525
Hi Marc:
The reason why I feel this should be changes to reflect the actual called function name is that one of main uses of the FUNCTION constant it to refer back to the function that is currently running, for the use recursive functions:
I still maintain the position that FUNCTION, as LINE and FILE, is supposed to be a lexical constant. (In retro-spective, adapting CLASS to do even more magic might have been a mistake, from a conceptual point of view.)
As with PHP 5.4 you are able change the function name and the class you
are running in thanks to traits, so makes sense to update these magic
constants to represent the the value at run time as apposed to compile
time. LINE and FILE will always be relating to values at
compile time.
Cheers,
Marc