| 제목 | get_instance()는 어떻게 구현된건가요? | ||
|---|---|---|---|
| 글쓴이 | milosz | 작성시각 | 2011/11/14 10:44:39 | 
| 
                         | 
                |||
                         주말에 공부하는 차에 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"); 배정하는 식으로만 보여서.. 이걸 어떻게 구현한지 궁금해서요 ㅠㅠㅎ 주말 내내 막막하네요.. ㅠㅠ  | 
                |||
| 다음글 | 페이지네이션 per_page 개수대로 리스트가 안나와요... (1) | ||
| 이전글 | $this->load->model('')이 ... (7) | ||
| 
                             
                                변종원(웅파)
                                /
                                2011/11/14 12:36:25 /
                                추천
                                0
                             
                             | 
                    
| 
                             
                                milosz
                                /
                                2011/11/14 12:39:28 /
                                추천
                                0
                             
                            
                                 ci를 열심히 본 결과, get_instance()가 클래스 외에서 먼저 선언된 부분이 있었습니다. 이런 형태로 싱글톤을 구현해주니 ci와 비스무리하게 구현이 되네요. ci에서 정말 편리하게 사용하던 부분인데 이 속에 이런 내용들이 있는지 전혀 모르고... ci가 짱입니다. 정말 ㅎ
 
                        
<?php
function & get_instance()
{
 return controller::get_instance();
}
class controller {
 private static $instance;
 function __construct(){
  self::$instance = & $this;
  $this->load = new loader();
 }
 public static function &get_instance(){
  return self::$instance;
 }
}
class loader{
 function model($model_name){
  $controller = & get_instance();
  $controller->$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();
   }
}
$c = new main();
$c->index();
                             | 
                    
| 
                             
                                한대승(불의회상)
                                /
                                2011/11/15 07:28:54 /
                                추천
                                0
                             
                            
                                CI의 get_instance()를 직접 구현 하셨군요. 
                        좋은 팁 감사 합니다. ^^ OpenSouce인 CI 자체가 훌륭한 교재임을 깜빡하고 있었네요. Milosz님께 감사 드립니다.  | 
                    
$this->load =newloader; 맨 위 컨트롤러에서 이렇게 선언했기에
$this->say->hello();// fatal error 에러이고
$this->load->say->hello(); 이건 실행되는거죠.ci에서 & get_instance() 역할은 ci 내부 객체를 컨트롤러, 모델, 뷰 이외에서 사용할 수 있도록 하기 위함입니다.