[CI代码解读] 告诉你为何libraries 方法引用命名规则需要大写

首先我们先从libraries本身的方法来看

/system/core/Loader.php 文件


public function library($library, $params = NULL, $object_name = NULL) { if (empty($library)) { return $this; } elseif (is_array($library)) { foreach ($library as $key => $value) { if (is_int($key)) { $this->library($value, $params); } else { $this->library($key, $params, $value); } } return $this; } if ($params !== NULL && ! is_array($params)) { $params = NULL; } $this->_ci_load_library($library, $params, $object_name); return $this; }

libraries 这个函数写的很大众化,基本意思就是首先参数是不是空呀。~ 然后就是因为可以传字符串,也可以传数组,既可以加载一个或多个类。然后首先他得判断下是不是数组。然后就是判断下有没有传输初始化参数(既第二个参数),随后就调用了第二个方法 function _ci_load_library

$class = ucfirst($class);

// Is this a stock library? There are a few special conditions if so ...
if (file_exists(BASEPATH.'libraries/'.$subdir.$class.'.php'))
{
   return $this->_ci_load_stock_library($class, $subdir, $params, $object_name);
}

第二个方法主要看这段代码,ucfirst 文件名大写就是在这里设置的。所以你的文件首字母必须大写。否则就报错喽。
最后就是 function _ci_load_stock_library 函数

if (file_exists($path = $path.'libraries/'.$file_path.$library_name.'.php'))
{
   // Override
include_once($path);

也是部分代码。
首先在第二个方法 _ci_load_stock_library 传入的一个参数 $class. _ci_load_stock_library方法接收后会直接引用你自定义的这个类。

libraries 引用自定义类就是这个样子的了。

所以说你可以 SmsClass 也可以 Smsclass 或者 SMSCLASS ,但就是不能 smsClass,个人感觉小驼峰好看,大驼峰不好看。

原创文章,作者:CrazyCodes,如若转载,请注明出处:https://blog.fastrun.cn/2017/02/08/1-16/

(0)
上一篇 2017年1月6日 下午2:18
下一篇 2017年3月9日 上午11:27

相关推荐

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

评论列表(1条)

  • Jimmy
    Jimmy 2018年12月2日 下午12:52

    Hi, i read your blog from time to time and i own a similar one and i was just wondering
    if you get a lot of spam feedback? If so how do you
    stop it, any plugin or anything you can recommend? I get so
    much lately it’s driving me crazy so any assistance is very much appreciated.
    I will right away snatch your rss as I can not in finding your email subscription hyperlink or newsletter service.
    Do you have any? Kindly permit me realize so that I may subscribe.
    Thanks. I’ve been browsing online more than three hours today,
    yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you did, the
    net will be a lot more useful than ever before.