Code: Select all
<?php
class foo {
public function __call($name, $args) {
echo "$name called";
}
}
$f = new foo();
$n = (string) 1;
$f->$n();
Moderator: General Moderators
Code: Select all
<?php
class foo {
public function __call($name, $args) {
echo "$name called";
}
}
$f = new foo();
$n = (string) 1;
$f->$n();
So, there might be a chance it will crash someday (i.e. future PHP versions)Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.
Code: Select all
$n = (string) 1;__call is a magic methodtimWebUK wrote:And, why you use underscores at the start of your function name? Is there documentation benefit that you find from doing this?
Because the method name must be a string, not an integer. I was leaning towards using this for efficiency reasons, but I ended up using strings (alpha-characters) in a way that is just as efficient, so it's good. I don't think that this would ever stop working in the future, but I'd rather be safe. That was my main concern.timWebUK wrote:I was wondering why you initialise the variable like this:
Code: Select all
$n = (string) 1;