2013-05-16 3 views
0
echo System\Core\Request::factory()->execute(); 

factory() вызывается первым, конструктор следующий, а execute() здесь последним. Это, конечно, работает так, как ожидалось.Статические методы - вызов функции

Класс запроса содержит пару атрибутов НЕСТАТИКИ. Я установил их в заводском методе. Как это:

public static function factory() 
{ 
    if(! Request::$initial) 
    { 
     $request = Request::$initial = Request::$current = new Request(); 
     $request->foo = 'bar'; 
    } 
    else 
    { 
     Request::$current = $request = new Request(); 
     $request->foo = 'aaa'; 
    } 
    return Request::$current; 
} 

Конструктор поставляется в следующем:

public function __construct() 
{ 
    echo $this->foo; // displays empty string 
    echo Request::$current->foo; // trying to get property of non-object 
} 

Что происходит?

ответ

2

Конструктор вызывается до того, как вы установили foo, потому что вы установили его на заводе после создания экземпляра запроса.

public static function factory() 
{ 
    if(! Request::$initial) 
    { 
     // constructor is called as part of this line 
     $request = Request::$initial = Request::$current = new Request(); 

     // foo is set AFTER the constructor is called 
     $request->foo = 'bar'; 
    } 
    else 
    { 
     // constructor is called as part of this line 
     Request::$current = $request = new Request(); 

     // foo is set AFTER the constructor is called 
     $request->foo = 'aaa'; 
    } 
    return Request::$current; 
} 
+0

Упс, спасибо ... – user2252786

Смежные вопросы