This question already has an answer here:
I am using Laravel and I get an error with the new
keyword. I'm still new to OOP concepets, what am I doing wrong?
Error:
Line 3: syntax error, unexpected 'new' (T_NEW)
Code:
class User extends Eloquent{
public static $user = new Fb();
public function mySnippets(){
return Snippet::all()->where('author','=',self::$user->getUser());
}
}
Thanks!
In addition to another answers,
Basically everything. You has both global state and static methods, which is bad. Just using static classes in order to wrap things doesn't mean you use OOP paradigm.
This is wrong. In terms of OOP, this should be similar to this one:
You can only initialize member variables with basic types.
You'll have to assign to the static variable after the class declaration, or provide a class-level
init
method or the like which does the assignment.You cannot use new in the declaration of attributes. So you will have to set it when you need it. As you might want to use it from static context, the constructor might not be appropriate (it only gets called when you use
new
on your class and you will have different instances ofFb
in each object).It could be better like this:
And then always retrieve the static attribute via