新浦京81707con > 注册购买 > 实例化类的一点摘记,php源码分析

原标题:实例化类的一点摘记,php源码分析

浏览次数:50 时间:2019-08-28

上面是调用三个模子(Module)的函数。这一个函数的基本作用是点名多个模型(抽象化为类)的名称,然后它会在模型目录上面寻找这么些类的脚本实例化以往回到。那样的做法有一点点好处正是载入和实例化是全自动的,你能够猎取最大的左右逢源。上面请看上边包车型客车代码,它并非常长何况不复杂:

看名称就能够想到其意义,装载器正是加载成分的,使用CI时,平时加载的有:

看名就会猜到其意义,装载器正是加载成分的,使用CI时,常常加载的有:

function &load_class($class_name, $param = null, $instantiate = true)
{
    static $objects = array();

$this->load->library()
$this->load->view()
$this->load->model()
$this->load->database()
$this->load->helper()
$this->load->config()
$this->load->add_package_path()

$this->load->library()
$this->load->view()
$this->load->model()
$this->load->database()
$this->load->helper()
$this->load->config()
$this->load->add_package_path()

    $class_name = ucfirst(strtolower($class_name));
    if (isset($objects[$class_name])) {
        return $objects[$class_name];
    }

复制代码 代码如下:

复制代码 代码如下:

    $class_file = DIR_MODELS . "/{$class_name}.inc.php";
    if (file_exists($class_file)) {
        require_once $class_file;

/**
 * Loader Class
 *
 * 客户加载views和files,常见的函数有model(),view(),library(),helper()
 *
 * Controller的好帮手,$this->load =& load_class('Loader', 'core');,加载了loader,Controller就最为强大了
 */
class CI_Loader {
 protected $_ci_ob_level;
 protected $_ci_view_paths  = array();
 protected $_ci_library_paths = array();
 protected $_ci_model_paths  = array();
 protected $_ci_helper_paths  = array();
 protected $_base_classes  = array(); // Set by the controller class
 protected $_ci_cached_vars  = array();
 protected $_ci_classes   = array();
 protected $_ci_loaded_files  = array();
 
 protected $_ci_models   = array();
 protected $_ci_helpers   = array();
 protected $_ci_varmap   = array('unit_test' => 'unit',
           'user_agent' => 'agent');
 public function __construct()
 {      
                //获取缓冲嵌套等第
  $this->_ci_ob_level  = ob_get_level();
  //library路径
                $this->_ci_library_paths = array(APPPATH, BASEPATH);
                //helper路径
  $this->_ci_helper_paths = array(APPPATH, BASEPATH);
                //model路径
  $this->_ci_model_paths = array(APPPATH);
                //view路径
  $this->_ci_view_paths = array(APPPATH.'views/' => TRUE);
  log_message('debug', "Loader Class Initialized");
 }

/**
* Loader Class
*
* 顾客加载views和files,常见的函数有model(),view(),library(),helper()
*
* Controller的好助手,$this->load =& load_class('Loader', 'core');,加载了loader,Controller就可是强大了
*/
class CI_Loader {
protected $_ci_ob_level;
protected $_ci_view_paths= array();
protected $_ci_library_paths= array();
protected $_ci_model_paths= array();
protected $_ci_helper_paths= array();
protected $_base_classes= array(); // Set by the controller class
protected $_ci_cached_vars= array();
protected $_ci_classes= array();
protected $_ci_loaded_files= array();

        if (!class_exists($class_name)) {
            return false;
        } else {
            $objects[$class_name] =& new $class_name($param);
            return $objects[$class_name];
        }

 //

 /**
  * 初始化Loader
  *
  */
 public function initialize()
 {
  $this->_ci_classes = array();
  $this->_ci_loaded_files = array();
  $this->_ci_models = array();
                //将is_loaded(common中著录加载宗旨类函数)加载的宗旨类交给_base_classes
  $this->_base_classes =& is_loaded();
                //加载autoload.php配置中文件
  $this->_ci_autoloader();
  return $this;
 }

protected $_ci_models= array();
protected $_ci_helpers= array();
protected $_ci_varmap= array('unit_test' => 'unit',
'user_agent' => 'agent');
public function __construct()
{
//获取缓冲嵌套等级
$this->_ci_ob_level = ob_get_level();
//library路径
$this->_ci_library_paths = array(APPPATH, BASEPATH);
//helper路径
$this->_ci_helper_paths = array(APPPATH, BASEPATH);
//model路径
$this->_ci_model_paths = array(APPPATH);
//view路径
$this->_ci_view_paths = array(APPPATH.'views/'=> TRUE);
log_message('debug', "Loader Class Initialized");
}

    }  else {
        if ($instantiate) {
            $objects[$class_name] = null;
        } 
        return null;
    }
}函数独有多少个参数,分别是 $class_name 、$param 以及 $instaniate ,个中 $param 是构造函数的参数,$instaniate 是可选的。请留神函数中的 $objects 数组是八个静态变量,也等于当调用完那一个函数的时候数组并不会自由,下一次调用此函数时这么些数组的数据是会保留的。那样做的益处正是能够将许多的类实例了之后,如必要再行调用则一向回到这几个类的实例就足以了,幸免了再一次调用,提升了质量。代码如下:

 //

 /**
  * 检验类是或不是加载
  */
 public function is_loaded($class)
 {
  if (isset($this->_ci_classes[$class]))
  {
   return $this->_ci_classes[$class];
  }
  return FALSE;
 }

//

/**
* 初始化Loader
*
*/
public function initialize()
{
$this->_ci_classes = array();
$this->_ci_loaded_files = array();
$this->_ci_models = array();
//将is_loaded(common中著录加载宗旨类函数)加载的中坚类交给_base_classes
$this->_base_classes =& is_loaded();
//加载autoload.php配置汉语件
$this->_ci_autoloader();
return $this;
}

    static $objects = array();

 //

 /**
  * 加载Class
  */
 public function library($library = '', $params = NULL, $object_name = NULL)
 {
  if (is_array($library))
  {
   foreach ($library as $class)
   {
    $this->library($class, $params);
   }
   return;
  }
                //假若$library为空大概已经加载。。。
  if ($library == '' OR isset($this->_base_classes[$library]))
  {
   return FALSE;
  }
  if ( ! is_null($params) && ! is_array($params))
  {
   $params = NULL;
  }
  $this->_ci_load_class($library, $params, $object_name);
 }

//

/**
* 检验类是还是不是加载
*/
public function is_loaded($class)
{
if (isset($this->_ci_classes[$class]))
{
return $this->_ci_classes[$class];
}
return FALSE;
}

    if (isset($objects[$class_name])) {
        return $objects[$class_name];
    }另外继续的代码就是检查实验是还是不是有其一类名称的文本,假诺有载入这些文件并招来钦点名称的类,如找到了那个类现在就实例化。这供给脚本中类的称呼必需和本子的文书名是一致的。作者想那也可以有益未来的代码处理。

 //

 /**
  * 加载和实例化model
  */
 public function model($model, $name = '', $db_conn = FALSE)
 {
                //CI辅助数组加载四个model
  if (is_array($model))
  {
   foreach ($model as $babe)
   {
    $this->model($babe);
   }
   return;
  }
  if ($model == '')
  {
   return;
  }
  $path = '';
  // 是不是留存子目录
  if (($last_slash = strrpos($model, '/')) !== FALSE)
  {
   // The path is in front of the last slash
   $path = substr($model, 0, $last_slash 1);
   // And the model name behind it
   $model = substr($model, $last_slash 1);
  }
  if ($name == '')
  {
   $name = $model;
  }
  if (in_array($name, $this->_ci_models, TRUE))
  {
   return;
  }
  $CI =& get_instance();
  if (isset($CI->$name))
  {
   show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
  }
  $model = strtolower($model); //model文件名全小写
  foreach ($this->_ci_model_paths as $mod_path)
  {
   if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
   {
    continue;
   }
   if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
   {
    if ($db_conn === TRUE)
    {
     $db_conn = '';
    }
    $CI->load->database($db_conn, FALSE, TRUE);
   }
   if ( ! class_exists('CI_Model'))
   {
    load_class('Model', 'core');
   }
   require_once($mod_path.'models/'.$path.$model.'.php');
   $model = ucfirst($model);
   $CI->$name = new $model();
                        //保存在Loader::_ci_models中,未来能够用它来判别有些model是还是不是早就加载过。
   $this->_ci_models[] = $name;
   return;
  }
  // couldn't find the model
  show_error('Unable to locate the model you have specified: '.$model);
 }

//

/**
* 加载Class
*/
public function library($library = '', $params = NULL, $object_name = NULL)
{
if (is_array($library))
{
foreach ($library as $class)
{
$this->library($class, $params);
}
return;
}
//假诺$library为空也许已经加载。。。
if ($library == '' OR isset($this->_base_classes[$library]))
{
return FALSE;
}
if ( ! is_null($params) && ! is_array($params))
{
$params = NULL;
}
$this->_ci_load_class($library, $params, $object_name);
}

$instaniate 参数那一年就发挥了职能,那么些参数会报告函数假如未找到则在 $objects 下边做四个符号位(null)幸免函数又再度的探求文件名同等看待复载入和探寻。

 //

 /**
  * 数据库Loader
  */
 public function database($params = '', $return = FALSE, $active_record = NULL)
 {
  // Grab the super object
  $CI =& get_instance();
  // 是或不是要求加载db
  if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
  {
   return FALSE;
  }
  require_once(BASEPATH.'database/DB.php');
  if ($return === TRUE)
  {
   return DB($params, $active_record);
  }
  // Initialize the db variable.  Needed to prevent
  // reference errors with some configurations
  $CI->db = '';
  // Load the DB class
  $CI->db =& DB($params, $active_record);
 }

//

/**
* 加载和实例化model
*/
public function model($model, $name = '', $db_conn = FALSE)
{
//CI协助数组加载多少个model
if (is_array($model))
{
foreach ($model as $babe)
{
$this->model($babe);
}
return;
}
if ($model == '')
{
return;
}
$path = '';
// 是不是留存子目录
if (($last_slash = strrpos($model, '/')) !== FALSE)
{
// The path is in front of the last slash
$path = substr($model, 0, $last_slash 1);
// And the model name behind it
$model = substr($model, $last_slash 1);
}
if ($name == '')
{
$name = $model;
}
if (in_array($name, $this->_ci_models, TRUE))
{
return;
}
$CI =& get_instance();
if (isset($CI->$name))
{
show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
}
$model = strtolower($model); //model文件名全小写
foreach ($this->_ci_model_paths as $mod_path)
{
if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
{
continue;
}
if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
{
if ($db_conn === TRUE)
{
$db_conn = '';
}
$CI->load->database($db_conn, FALSE, TRUE);
}
if ( ! class_exists('CI_Model'))
{
load_class('Model', 'core');
}
require_once($mod_path.'models/'.$path.$model.'.php');
$model = ucfirst($model);
$CI->$name = new $model();
//保存在Loader::_ci_models中,以往能够用它来剖断某些model是或不是早就加载过。
$this->_ci_models[] = $name;
return;
}
// couldn't find the model
show_error('Unable to locate the model you have specified: '.$model);
}

    $class_file = DIR_MODELS . "/{$class_name}.inc.php";
    if (file_exists($class_file)) {
        require_once $class_file;

 //

 /**
  * 加载数据库工具类
  */
 public function dbutil()
 {
  if ( ! class_exists('CI_DB'))
  {
   $this->database();
  }
  $CI =& get_instance();
  // for backwards compatibility, load dbforge so we can extend dbutils off it
  // this use is deprecated and strongly discouraged
  $CI->load->dbforge();
  require_once(BASEPATH.'database/DB_utility.php');
  require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
  $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
  $CI->dbutil = new $class();
 }

//

/**
* 数据库Loader
*/
public function database($params = '', $return = FALSE, $active_record = NULL)
{
// Grab the super object
$CI =& get_instance();
// 是或不是须求加载db
if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
{
return FALSE;
}
require_once(BASEPATH.'database/DB.php');
if ($return === TRUE)
{
return DB($params, $active_record);
}
// Initialize the db variable. Needed to prevent
// reference errors with some configurations
$CI->db = '';
// Load the DB class
$CI->db =& DB($params, $active_record);
}

        if (!class_exists($class_name)) {
            return false;
        } else {
            $objects[$class_name] =& new $class_name($param);
            return $objects[$class_name];
        }

 //

 /**
  * Load the Database Forge Class
  *
  * @return string
  */
 public function dbforge()
 {
  if ( ! class_exists('CI_DB'))
  {
   $this->database();
  }
  $CI =& get_instance();
  require_once(BASEPATH.'database/DB_forge.php');
  require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
  $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
  $CI->dbforge = new $class();
 }

//

/**
* 加载数据库工具类
*/
public function dbutil()
{
if ( ! class_exists('CI_DB'))
{
$this->database();
}
$CI =& get_instance();
// for backwards compatibility, load dbforge so we can extend dbutils off it
// this use is deprecated and strongly discouraged
$CI->load->dbforge();
require_once(BASEPATH.'database/DB_utility.php');
require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
$class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
$CI->dbutil = new $class();
}

本文由新浦京81707con发布于注册购买,转载请注明出处:实例化类的一点摘记,php源码分析

关键词: 新浦京81707con

上一篇:微信小程序实现城市列表选择,城市切换

下一篇:没有了