7

Is it illegal to assign some object to static property?

I am getting HTTP 500 error in below code.

require_once('class.linkedlist.php');

class SinglyLinkedlistTester {
    public static $ll = new Linklist();
}

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request.

Note: No issue with non object like string,int assignment to static variable. As an example,

public static $ll = 5; //no issue

Also there is no code issue in class.linkedlist.php.

P K
  • 9,972
  • 12
  • 53
  • 99

2 Answers2

10

You can't create new objects in class property declarations. You have to use the constructor to do this:

class SinglyLinkedlistTester {
    public static $ll;

    public function __construct() {
        static::$ll = new Linklist();
    }
}

Edit: Also, you can test your files for errors without executing them using PHP's lint flag (-l):

php -l your_file.php

This will tell you whether there are syntax or parsing errors in your file (in this case, it was a parse error).

FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
  • @Josh: Actually, [this](http://www.php.net/manual/en/language.oop5.late-static-bindings.php) is probably more helpful. – FtDRbwLXw6 Jan 25 '12 at 20:01
  • 1
    @drrcknlsn, http://stackoverflow.com/questions/151969/php-self-vs-this I guess either would work in this case, but self would probably be better as the property is static, and using `$this` in a static method with an object being instanciated could cause problems. – mowwwalker Jan 25 '12 at 20:03
  • 2
    @drrcknlsn Definitely helpful. I was referring to this specifically, *"Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to **an object**."* Seems appropriate for what he was attempting to do. – Josh Jan 25 '12 at 20:06
  • @ All, anything to do with late static binding in my case? late static binding is related to inheritance and context of resolution. – P K Jan 25 '12 at 20:11
  • @Walkerneo: I did not use `$this`, I used `static`, and I used it intentionally. Reversed your edit. Please see my comment to @Josh for a link describing late static binding. – FtDRbwLXw6 Jan 25 '12 at 20:21
  • @drrcknlsn, i didn't understand use of constructor, suppose i want SinglyLinkedlist::some_static_method(). I am not instantiating any object in this case, so how come static property will be initialized without constructor call? let's assume i am using $ll inside some_static_method(). – P K Jan 25 '12 at 20:27
  • @Praveen: If your class is not meant to be instantiated, then `__construct()` will obviously not work. You'll have to initialize `$ll` with some static method (eg. `SinglyLinkedlistTester::init()` or similar). – FtDRbwLXw6 Jan 25 '12 at 20:33
  • @drrcknlsn, oops, my mistake. I thought it was a typo, I'd never seen the static variable before. – mowwwalker Jan 25 '12 at 23:09
  • @drrcknlsn, but if a class is static itself then constructor your solution above will not work, correct me if I am wrong. – Phantom007 Jun 28 '13 at 06:30
  • @Shouvik: I answered that question already in previous comments. – FtDRbwLXw6 Jun 28 '13 at 12:56
5

you should take care, that you don't override the static property on each instantiation of a object, therefore do:

class SinglyLinkedlistTester {
    private static $ll;

    public function __construct() {
        if (!self::$ll) self::$ll = new Linklist();
    }
}
staabm
  • 1,535
  • 22
  • 20