주말에 공부하는 차에 CI의 구조를 연구해보자는 마음에 코드를 작성해봤는데 & get_instance() 로 값을 받아오는 부분을 도무지 이해를 못하겠더군요 ;ㅅ;
<?php
class controller {
function __construct(){
$this->load = new loader;
}
}
class loader{
function model($model_name){
$this->$model_name = new $model_name;
}
}
class say{
function hello(){
echo "hello";
}
}
class main extends controller {
function __construct(){
parent::__construct();
$this->load->model("say");
}
function index(){
$this->say->hello(); // fatal error
$this->load->say->hello();
}
}
$c = new main();
$c->index();
?>
ci를 살짝 컨닝했더니 get_instance로 본 클래스를 포인터로 가져와서 붙이는 방식인 것 같은데 위 소스에서 아무리 적용을 하려고 해봐도 get_instance()가 없다고만 할 뿐 클래스를 배정받지 못하더라구요... 싱글톤인 것 같기도 한데.. 뭔가 막막합니다 ;ㅅ;
싱글톤이랑 비슷한 것 같아서 검색해봐도 return new $model; 식으로 수정하고 $this->say = $this->load->model("say"); 배정하는 식으로만 보여서.. 이걸 어떻게 구현한지 궁금해서요 ㅠㅠㅎ
주말 내내 막막하네요.. ㅠㅠ
$this->load =newloader; 맨 위 컨트롤러에서 이렇게 선언했기에
$this->say->hello();// fatal error 에러이고
$this->load->say->hello(); 이건 실행되는거죠.ci에서 & get_instance() 역할은 ci 내부 객체를 컨트롤러, 모델, 뷰 이외에서 사용할 수 있도록 하기 위함입니다.