PHP: Object assignment to static property, is it illegal?

后端 未结 2 882
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 03:16

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 Sin         


        
2条回答
  •  庸人自扰
    2021-01-18 03:41

    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).

提交回复
热议问题