I'm rather new in PHP and it turns out that I'm not able to find a solution for the following problem:
I have a simple class like this:
class User{
private $name;
function __construct($name){
$this->name = $name;
}
}
All I want to do is to define a static instance of it, like one of these:
public const UNKNOWN_USER = new User("unknown);
so that I can use this as a dummy everywhere, e.g.:
public static login($name){
if( /* userdoesnotexist */ ){
return UNKNOWN_USER;
}
}
and check for it - of course:
if( login($name) == UNKNOWN_USER){
/* I don't know you! */
}
I've tried the following:
$UNKNOWN_USER = new User("unknown");
/* $UNKNOWN_USER not available in class-methods */
define(UNKNOWN_USER, new User("unknown"));
/* not allowed */
class User{
const UNKNOWN_USER = new User("unknown");
/* "new" leads to a syntax error */
}