2016-07-11 1 views
0

개발 중에는 작동하지만 프로덕션에서는 작동하지 않는 약간의 코드에 약간의 문제가 있습니다. 다른 모든 코드가 작동하기 때문에 이상하게 보입니다. - 난 믿을 수 없어 내가 찾을 수있는 다른 유사한 문제에서

Fatal error: Using $this when not in object context in /[snip]/application/modules/manage_plugins/models/Manage_plugins.php on line 6 A PHP Error was encountered

Severity: Error

Message: Using $this when not in object context

Filename: models/Manage_plugins.php

Line Number: 6

Backtrace:

, 그것은 "$이"정적 컨텍스트에서 사용하려고 시도하는 사람들로 인해되었다

전체 오류는 다음이다 나를위한 경우입니다. 여기

생성자의 첫 번째 인 6 호선 (오류 라인)과 함께 manage_plugins 생성자입니다 :

class Manage_plugins extends CI_Model { 

    public function __construct() { 
     $this->mediator->attach("manage_load", function($name, $data) { $this->on_manage_load(); }); 

     $this->load->model("automediator"); 
    } 

} 

그것은 다음 코드에 의해로드 (명시 적으로 호출되지 않음)되는 :

$CI =& get_instance(); 

$CI->load->model("manage_plugins/manage_plugins"); 

왜 이런 일이 일어나는 지 알고 계십니까?

+1

중복이 가능합니까? http://stackoverflow.com/questions/8391099/using-this-in-anonymous-function – rexmac

답변

0

rexmarc 덕분에 문제를 해결할 수 있었고 use- 익명 함수로 $this 복사본으로 PHP 5.3에서 비슷한 구조로 작업 할 수있었습니다.

나는 변경 다음

class Manage_plugins extends CI_Model { 
    public function __construct() { 
     $this->mediator->attach("manage_load", function($name, $data) { $this->on_manage_load(); }); 

     $this->load->model("automediator"); 
    } 

} 

로 :

class Manage_plugins extends CI_Model { 
    public function __construct() { 
     $me =& $this; 
     $this->mediator->attach("manage_load", function($name, $data) use($me) { $me->on_manage_load(); }); 

     $this->load->model("automediator"); 
    } 

} 

이것에 대한 또 다른 해결책은 수 있었다 :이 문제는 이전 때문에 PHP 버전에서 발생 된

class Manage_plugins extends CI_Model { 
    public function __construct() { 
     $this->mediator->attach("manage_load", [$this, 'on_manage_load']); 

     $this->load->model("automediator"); 
    } 

} 

5.4, ​​$this은 익명 기능을 사용할 수 없습니다.

5.4.0 - Anonymous functions may use $this, as well as be declared statically

자료 : http://php.net/manual/en/functions.anonymous.php

는 문제는 개발 (5.5), 생산 (5.3)에 다른 PHP 버전의 주목했다.

도 참조하십시오. https://stackoverflow.com/a/19432335/3649573

관련 문제