,
나는 코드가 $CI =& get_instance();
사람이 코드가 어떻게 작동하는지 설명해 주 시겠어요보고 계속?
나는 $ CI 슈퍼 오브젝트에 대한 참조를 리턴하지만, get_instance()
은 어디서 왔는가?
,
나는 코드가 $CI =& get_instance();
사람이 코드가 어떻게 작동하는지 설명해 주 시겠어요보고 계속?
나는 $ CI 슈퍼 오브젝트에 대한 참조를 리턴하지만, get_instance()
은 어디서 왔는가?
기본적으로 정적 메서드 대신 함수를 사용하는 Singleton Design Pattern입니다. 지금은, 사실 ...
편집을
는 싱글을 적용하지 않는, 그래서 기본적으로 source code
을 확인, 깊은 찾으려면,하지만 공공 기능에 대한 바로 가기입니다 알다. PHP4와의 호환성을 위해 참조를 올바르게 반환하려면 double-global-variable-hack을 사용해야했습니다. 그렇지 않으면 참조가 엉망이 될 것입니다. 그리고 PHP4는 정적 인 메소드 (어쨌든 제대로)를 지원하지 않았기 때문에이 함수를 사용하는 것이 더 나은 방법이었습니다. 그래서 여전히 앱이 PHP5는 경우
그래서, 가 당신이 할 수 있습니다 어떤 경우에는, 그것은 동일한의 대신CI_Base::get_instance();
을하고 아무 잘못 ...
get_instance()는 CodeIgniter의 코어 파일에 정의 된 함수입니다. 슈퍼 객체 외부의 스코프에있을 때이 코드를 사용하여 CodeIgniter 슈퍼 객체에 대한 싱글 톤 참조를 얻습니다.
나는 그것이 base.php 또는 유사한 것으로 정의되어 있다고 확신합니다.
이 없어야합니다 ... 레거시 이유로 존재 컨트롤러와 별개로 존재하지만 코드 이그니 터의 모든 리소스를 활용할 수있는 클래스를 개발하십시오. 이것은 get_instance() 함수를 사용하면 쉽게 가능합니다.
컨트롤러 함수 내에서 인스턴스화하는 모든 클래스는 get_instance() 함수를 사용하여 코드 이그니 터의 네이티브 리소스에 액세스 할 수 있습니다. 이 함수는 주 코드 Igniter 객체를 반환합니다.
$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');
은 $이은, 그러나, 내
작동 단지 등 :
는일반적으로 사용 가능한 코드 점화 함수를 호출 할 수는 은 $이 이 구조 사용해야합니다 컨트롤러, 모델 또는 뷰를 선택합니다.
는첫째, 변수에 코드 점화 객체를 할당합니다 :
$obj =& get_instance();
당신은 할당 한 후에는 사용자 정의 클래스 내에서 코드 점화의 클래스를 사용하고자하는 경우에는 다음과 같이 당신은 그렇게 할 수 있습니다 변수에 반대, 대신 의 변수를 사용합니다 $이 :
$obj =& get_instance();
$obj->load->helper('url');
$obj->load->library('session');
$obj->config->item('base_url');
등
참고 : 당신은 N거야 상기 get_instance() 함수 참조로 전달되는 것을 otice이 매우 중요
$obj =& get_instance();
. 참조로 지정하면 원래 코드 이그니 터 객체의 복사본을 만드는 대신이 코드 객체를 사용할 수 있습니다.
당신이이 CodeIgniter는이 라이브러리와 클래스를로드하는 방법을 이해하는 싱글 구조 here
에서이 기능에 대해 읽을 수 있습니다
<?php
/*
====================================
start of the loader class
====================================
*/
class Loader {
protected function _init_class($class){
$C = Controller::get_instance();
$name = strtolower($class);
$C->$name = new $class();
}
public function _class($library){
if(is_array($library)){
foreach($library as $class){
$this->library($class);
}
return;
}
if($library == ''){
return false;
}
$this->_init_class($library);
}
public function view ($param) {
echo $param;
}
}
/*
===============================
End of the loader class
==============================
*/
/*
===============================
start of core controller class
==============================
*/
class Controller {
private static $instance;
function __construct() {
self::$instance = $this;
$this->load = new Loader();
}
public static function get_instance(){
return self::$instance;
}
}
/*
===============================
end of the core controller class
===================================
*/
/*
====================================================
start of library sections (put all your library classes in this section)
=====================================================
*/
class MyLibrary {
private $c;
function __construct() {
$this->c = Controller::get_instance();
}
function say($sentence) {
$this->c->load->view($sentence);
}
}
/*
====================================================
End of the library sections
====================================================
*/
/*
============================================
start of controller section (put all your controller classes in this section)
===========================================
*/
class Foo extends Controller {
function __construct() {
parent::__construct();
$this->load->_class('MyLibrary');
}
function bar() {
$this->mylibrary->say('Hello World');
}
}
/*
==========================================
End of the controller sections
==========================================
*/
$foo = new Foo();
$foo->bar();
CI_Controller를 확장 만하는 클래스, 모델,보기 사용할 수 있습니다
$this->load->library('something');
$this->load->helper('something');//..etc
맞춤 클래스는 위 코드를 사용할 수 없습니다. 당신의 사용자 정의 클래스에 예를 들어
$CI=&get instance();
$CI->load->library('something');
$CI->load->helper('something');
를 사용해야합니다 는 사용자 정의 클래스에서 위의 기능을 사용하려면
// this following code will not work
Class Car
{
$this->load->library('something');
$this->load->helper('something');
}
//this will work
Class Car
{
$CI=&get_instance();
$CI->load->library('something');
$CI->load->helper('something');
}
// Here $CI is a variable.
때 CI 슈퍼 객체와 이유를 사용하는? CI 수퍼 객체에 관한 CI 문서를 가르쳐 주시겠습니까? – Girish
소스 코드에 대한 링크는 404. – Bugfixer
+1은 실제로'$ CI = & get_instance();에 대한 ** 대체 ** 사용법을 가리키고 있습니다. 나는 그 문서를 찾고있는 사람에게 내 얼굴을 두드리고있었습니다 ... – HomeOffice