2015-01-13 2 views
2

컨트롤러 constructori에 생성자와 메서드 aas이 있습니다. 나는 새 개체 $col = new constructori()를 생성하고, 그 후 나는 방법에게 전화 echo $col->aas();Codeigniter의 생성자

Class Constructori { 

    function index(){ 

    } 

    public function __construct(){ 

     echo" something <br />"; 
    } 

    function aas(){ 
     echo 'another something <br>'; 
    } 



} 

$col = new Constructori(); 

echo $col->aas(); 
내가 갖는 이유

사람이 설명 할 수 :

something 

another something 

something 

대신

something 

something 

another something 
+0

내가 프레임 워크 (CI)가 이미 컨트롤러를 인스턴스화되어 가정합니다. –

+0

나는 알고있다. 출력은'''s, s, a''라고 기대할 것이다. 출력의 두 번째와 세 번째 라인은 나를 혼란스럽게합니다. – Napster33

답변

1

의 출력이 예상됩니다. 먼저 실행합니다 :

$col = new Constructori();//something 
echo $col->aas();//another something 
//Now codeigniter itself try to create new controller ---thats why you got something 

이유는 Codeigniter가 모든 클래스를 먼저로드하기 때문입니다. 그런 다음 필요한 클래스 객체를 만듭니다. 그래서 클래스를로드 할 때 Constructori 코드가 먼저 실행됩니다. 마침내 Codeigniter는 Constructori 개체를 만듭니다.

은 당신의 코드는 다음과 같다 가정 :

$col = new Constructori(); 
echo $col->aas(); 

$col2 = new Constructori(); 
echo $col2->aas(); 

출력 될 것입니다 :

something  //for $col construct 
    another something //for $col->aas(); 

    something //for $col2 construct 
    another something // $col2->aas(); 

    something //last Codeigniter creates one 
+0

잘 알고있어서, 나는 실행의 순서가 다른 방향이었던 것 같았습니다. –

+0

고마워요! 나는 codeigniter가 처음으로 코드를 생성하고 나머지 코드가 실행되었다고 생각했다. – Napster33

+0

실제로 클래스는 객체를 생성하기 전에로드되어야합니다. 코드 서명자가 클래스를로드하려고 할 때도 다른 코드도 실행합니다. –