I have the class.
Class User {
private $_name;
private $_email;
public static function factory() {
return new __CLASS__;
}
public function test() {
}
}
and when i make a static method call using the syntax below.
User::factory();
it throws me following syntax error.
Parse error: syntax error, unexpected T_CLASS_C in htdocs/test/index.php on line 8
the error is being thrown because the Static factory() method is unable to create the object during the static method call.
and when i change the magic constant __CLASSS__
to the name of the current class i.e to User
then it works.
what am i missing?
Try:
Class User {
private $_name;
private $_email;
public static function factory() {
$class = __CLASS__;
return new $class;
}
public function test() {
}
}
Not really sure why your example doesn't works. But what does work is:
public static function factory()
{
return new self();
}
Try this:
$class = __CLASS__;
return new $class;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With