Ok, this is very unexpected and it annoys me. I have function called default() in all my classes. But, I can’t call it for some reason. I guess it’s because of ‘default:’ in switch statement (which I don’t use at all).
Is this really true that I can’t name my function default() in php or am I doing something wrong?
Here’s the error:
Parse error: syntax error, unexpected T_DEFAULT, expecting T_STRING in
C:\Users\Jinxed\Desktop\WebTrgovina\app\c\Controller.c.php on line 8
And here’s the code in question:
<?php
abstract class Controller {
protected $view;
function __construct() {
}
abstract function default();
function getView(){
return $this->view;
}
}
Code works fine when I rename default to something else, but this is annoying, is there any solution to have function named default()?
You cannot use a reserved word as a class function. You can, however, use __call:
Which will permit you to do
$foo->default(). A better idea though would be to not choose an implementation/api that relies upon using reserved words for method names.