用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - php代码库

CI框架service层

2014-10-31 作者: php源代码大全举报

[php]代码库

<?php

class MY_Loader extends CI_Loader
{
    /**
	 * List of loaded sercices
	 *
	 * @var array
	 * @access protected
	 */
	protected $_ci_services = array();
	/**
	 * List of paths to load sercices from
	 *
	 * @var array
	 * @access protected
	 */
	protected $_ci_service_paths		= array();
    
    /**
     * Constructor
     * 
     * Set the path to the Service files
     */
    public function __construct()
    {
        
        parent::__construct();
        
        $this->_ci_service_paths = array(APPPATH);
    }
    
    /**
     * Service Loader
     * 
     * This function lets users load and instantiate classes.
	 * It is designed to be called from a user's app controllers.
	 *
	 * @param	string	the name of the class
	 * @param	mixed	the optional parameters
	 * @param	string	an optional object name
	 * @return	void
     */
    public function service($service = '', $params = NULL, $object_name = NULL)
    {
        if(is_array($service))
        {
            foreach($service as $class)
            {
                $this->service($class, $params);
            }
            
            return;
        }
        
        if($service == '' or isset($this->_ci_services[$service])) {
            return FALSE;
        }
 
        if(! is_null($params) && ! is_array($params)) {
            $params = NULL;
        }
        
        $subdir = '';

        // Is the service in a sub-folder? If so, parse out the filename and path.
        if (($last_slash = strrpos($service, '/')) !== FALSE)
        {
                // The path is in front of the last slash
                $subdir = substr($service, 0, $last_slash + 1);

                // And the service name behind it
                $service = substr($service, $last_slash + 1);
        }
        
        foreach($this->_ci_service_paths as $path)
        {
            
            $filepath = $path .'service/'.$subdir.$service.'.php';
            
            if ( ! file_exists($filepath))
            {
                continue;
            }
            
            include_once($filepath);
            
            $service = strtolower($service);

            if (empty($object_name))
            {
                $object_name = $service;
            }
            
            $service = ucfirst($service);
            $CI = &get_instance();
            if($params !== NULL)
            {
                $CI->$object_name = new $service($params);
            }
            else
            {
                $CI->$object_name = new $service();
            }
            
            $this->_ci_services[] = $object_name;
            
            return;
        }
    }
    
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...