Optimize get_instance() calls/assignments
diff --git a/system/core/Loader.php b/system/core/Loader.php
index daf326f..7817258 100644
--- a/system/core/Loader.php
+++ b/system/core/Loader.php
@@ -642,8 +642,7 @@
*/
public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE)
{
- $CI =& get_instance();
- return $CI->config->load($file, $use_sections, $fail_gracefully);
+ return get_instance()->config->load($file, $use_sections, $fail_gracefully);
}
// --------------------------------------------------------------------
diff --git a/system/core/Model.php b/system/core/Model.php
index 1eb6f90..11e6075 100644
--- a/system/core/Model.php
+++ b/system/core/Model.php
@@ -59,8 +59,7 @@
*/
public function __get($key)
{
- $CI =& get_instance();
- return $CI->$key;
+ return get_instance()->$key;
}
}
diff --git a/system/database/DB_utility.php b/system/database/DB_utility.php
index 9f953d4..6656159 100644
--- a/system/database/DB_utility.php
+++ b/system/database/DB_utility.php
@@ -282,8 +282,7 @@
extract($params);
// Load the xml helper
- $CI =& get_instance();
- $CI->load->helper('xml');
+ get_instance()->load->helper('xml');
// Generate the result
$xml = '<'.$root.'>'.$newline;
diff --git a/system/helpers/cookie_helper.php b/system/helpers/cookie_helper.php
index e465412..5cdcdd1 100644
--- a/system/helpers/cookie_helper.php
+++ b/system/helpers/cookie_helper.php
@@ -59,8 +59,7 @@
function set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
{
// Set the config file options
- $CI =& get_instance();
- $CI->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure, $httponly);
+ get_instance()->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure, $httponly);
}
}
@@ -77,9 +76,8 @@
*/
function get_cookie($index, $xss_clean = FALSE)
{
- $CI =& get_instance();
$prefix = isset($_COOKIE[$index]) ? '' : config_item('cookie_prefix');
- return $CI->input->cookie($prefix.$index, $xss_clean);
+ return get_instance()->input->cookie($prefix.$index, $xss_clean);
}
}
diff --git a/system/helpers/html_helper.php b/system/helpers/html_helper.php
index ece3958..988eee7 100644
--- a/system/helpers/html_helper.php
+++ b/system/helpers/html_helper.php
@@ -199,15 +199,13 @@
{
if ($k === 'src' && strpos($v, '://') === FALSE)
{
- $CI =& get_instance();
-
if ($index_page === TRUE)
{
- $img .= ' src="'.$CI->config->site_url($v).'"';
+ $img .= ' src="'.get_instance()->config->site_url($v).'"';
}
else
{
- $img .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
+ $img .= ' src="'.get_instance()->config->slash_item('base_url').$v.'"';
}
}
else
diff --git a/system/helpers/language_helper.php b/system/helpers/language_helper.php
index 4d571a7..d7aa8e6 100644
--- a/system/helpers/language_helper.php
+++ b/system/helpers/language_helper.php
@@ -52,8 +52,7 @@
*/
function lang($line, $for = '', $attributes = array())
{
- $CI =& get_instance();
- $line = $CI->lang->line($line);
+ $line = get_instance()->lang->line($line);
if ($for !== '')
{
diff --git a/system/helpers/security_helper.php b/system/helpers/security_helper.php
index 4bb94a2..7a6df54 100644
--- a/system/helpers/security_helper.php
+++ b/system/helpers/security_helper.php
@@ -49,8 +49,7 @@
*/
function xss_clean($str, $is_image = FALSE)
{
- $CI =& get_instance();
- return $CI->security->xss_clean($str, $is_image);
+ return get_instance()->security->xss_clean($str, $is_image);
}
}
@@ -66,8 +65,7 @@
*/
function sanitize_filename($filename)
{
- $CI =& get_instance();
- return $CI->security->sanitize_filename($filename);
+ return get_instance()->security->sanitize_filename($filename);
}
}
@@ -107,8 +105,7 @@
*/
function strip_image_tags($str)
{
- $CI =& get_instance();
- return $CI->security->strip_image_tags($str);
+ return get_instance()->security->strip_image_tags($str);
}
}
diff --git a/system/helpers/url_helper.php b/system/helpers/url_helper.php
index b0f4368..2d92897 100644
--- a/system/helpers/url_helper.php
+++ b/system/helpers/url_helper.php
@@ -91,8 +91,7 @@
*/
function current_url()
{
- $CI =& get_instance();
- return $CI->config->site_url($CI->uri->uri_string());
+ return get_instance()->config->site_url($CI->uri->uri_string());
}
}
@@ -109,8 +108,7 @@
*/
function uri_string()
{
- $CI =& get_instance();
- return $CI->uri->uri_string();
+ return get_instance()->uri->uri_string();
}
}
@@ -127,8 +125,7 @@
*/
function index_page()
{
- $CI =& get_instance();
- return $CI->config->item('index_page');
+ return get_instance()->config->item('index_page');
}
}
diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php
index f13e447..48c803d 100644
--- a/system/libraries/Cache/drivers/Cache_redis.php
+++ b/system/libraries/Cache/drivers/Cache_redis.php
@@ -208,7 +208,7 @@
{
$success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
}
-
+
if ( ! $success)
{
log_message('debug', 'Cache: Redis connection refused. Check the config.');
@@ -225,7 +225,7 @@
{
$this->_redis->auth($config['password']);
}
-
+
return TRUE;
}
diff --git a/system/libraries/Upload.php b/system/libraries/Upload.php
index 67a6550..525880f 100644
--- a/system/libraries/Upload.php
+++ b/system/libraries/Upload.php
@@ -263,6 +263,13 @@
*/
protected $_file_name_override = '';
+ /**
+ * CI Singleton
+ *
+ * @var object
+ */
+ protected $CI;
+
// --------------------------------------------------------------------
/**
@@ -279,6 +286,7 @@
}
$this->mimes =& get_mimes();
+ $this->CI =& get_instance();
log_message('debug', 'Upload Class Initialized');
}
@@ -479,8 +487,7 @@
}
// Sanitize the file name for security
- $CI =& get_instance();
- $this->file_name = $CI->security->sanitize_filename($this->file_name);
+ $this->file_name = $this->CI->security->sanitize_filename($this->file_name);
// Truncate the file name if it's too long
if ($this->max_filename > 0)
@@ -1081,7 +1088,7 @@
return FALSE;
}
- return get_instance()->security->xss_clean($data, TRUE);
+ return $this->CI->security->xss_clean($data, TRUE);
}
// --------------------------------------------------------------------
@@ -1094,17 +1101,13 @@
*/
public function set_error($msg)
{
- $CI =& get_instance();
- $CI->lang->load('upload');
+ $this->CI->lang->load('upload');
- if ( ! is_array($msg))
- {
- $msg = array($msg);
- }
+ is_array($msg) OR $msg = array($msg);
foreach ($msg as $val)
{
- $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
+ $msg = ($this->CI->lang->line($val) === FALSE) ? $val : $this->CI->lang->line($val);
$this->error_msg[] = $msg;
log_message('error', $msg);
}
diff --git a/system/libraries/Xmlrpcs.php b/system/libraries/Xmlrpcs.php
index d263d78..50ff423 100644
--- a/system/libraries/Xmlrpcs.php
+++ b/system/libraries/Xmlrpcs.php
@@ -384,17 +384,13 @@
{
return call_user_func(array($this, $method_parts[1]), $m);
}
+ elseif ($this->object === FALSE)
+ {
+ return get_instance()->$method_parts[1]($m);
+ }
else
{
- if ($this->object === FALSE)
- {
- $CI =& get_instance();
- return $CI->$method_parts[1]($m);
- }
- else
- {
- return $this->object->$method_parts[1]($m);
- }
+ return $this->object->$method_parts[1]($m);
}
}
else
diff --git a/system/libraries/Zip.php b/system/libraries/Zip.php
index 229b1ec..250ee02 100644
--- a/system/libraries/Zip.php
+++ b/system/libraries/Zip.php
@@ -425,8 +425,7 @@
$filename .= '.zip';
}
- $CI =& get_instance();
- $CI->load->helper('download');
+ get_instance()->load->helper('download');
$get_zip = $this->get_zip();
$zip_content =& $get_zip;