-1

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!

intelis
  • 7,829
  • 14
  • 58
  • 102
  • It does matter. As for **non-static properties**, you want a new instance of the property in each object. Thus you can just assign it in the constructor. As for **static properties**, you want the same instance throughout all objects (and you want them to be available without having an object created). Thus, you cannot use the same solutions. – aufziehvogel Apr 03 '13 at 17:32
  • @Aufziehvogel the answers are subtly different, but both questions are due to not noticing the following from the [manual page](http://php.net/language.oop5.properties): "This declaration may include an initialization, but this initialization must be a constant value." IMO the best answer would cover both scenarios and justify the dupe claim. – cmbuckley Apr 03 '13 at 19:18
  • http://stackoverflow.com/questions/9009276/php-object-assignment-to-static-property-is-it-illegal – user3526905 Jan 15 '15 at 10:50

3 Answers3

4

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 of Fb in each object).

It could be better like this:

public static function getUser() {
   if (self::$user == null) self::$user = new Fb();
   return self::$user;
}

And then always retrieve the static attribute via

self::getUser()
aufziehvogel
  • 7,167
  • 5
  • 34
  • 56
  • hi, thanks for answer. One question though: is that if statement a beginning of a singelton pattern? I mean, can't I just ommit it? thanks – intelis Apr 03 '13 at 17:24
  • 1
    it's not a singleton pattern in the way it is taught (meaning the whole class will be created once), but it is "singleton" in the way that it makes sure that you only once assign a new object to a variable (and keep it all the time). If you removed the `if` condition, you'd have a different instance of `Fb` each time (losing previously called actions etc.). – aufziehvogel Apr 03 '13 at 17:28
  • This works perfectly, but this is a bad practice and something that should be always avoided. – Yang Apr 03 '13 at 17:36
2

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.

class User extends Eloquent {
  public static $user = null;

}

User::$user = new Fb();
user229044
  • 232,980
  • 40
  • 330
  • 338
  • Yes he can do this. But he should not. `User::$user = new FB();` is just a bad practice and breaks encapsulation. Static classes are fake-OOP as you might know. – Yang Apr 03 '13 at 17:40
1

In addition to another answers,

I'm still new to OOP concepets, what am I doing wrong?

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.

class User extends Eloquent
{
    public static $user = new Fb();

    public function mySnippets(){
        return Snippet::all()->where('author','=',self::$user->getUser());
    }

}

This is wrong. In terms of OOP, this should be similar to this one:

class User extends Eloquent
{
    private $fb;

    public function __construct(FB $fb)
    {
       $this->fb = $fb;
    }

    public function mySnippets()
    {
      return Snippet::all()->where('author', '=', $this->user->getUser());
    }
}

//Usage:

$fb = new FB();
$user = new User($fb);

var_dump($user->mySnippets());
Yang
  • 8,580
  • 8
  • 33
  • 58