php Codeigniter 无法加载库类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14300311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter unable to load library class
提问by Nitish
I have a library file named myMenu.php
我有一个名为的库文件 myMenu.php
<?php
class myMenu{
function show_menu()
{
$obj = & get_instance();
$obj->load->helper('url');
$menu = "<ul>";
$menu .= "<li>";
$menu .= anchor('books/index','List of books');
$menu .= "</li>";
$menu .= "<li>";
$menu .= anchor('books/input','Books entry');
$menu .= "</li>";
$menu .= "</ul>";
return $menu;
}
}
Now I loaded this library in my controller books.php
现在我在我的控制器中加载了这个库 books.php
function index()
{
$this->load->library('myMenu');
$menu = new myMenu;
$data['menu'] = $menu->show_menu();
$this->load->view('main_view',$data);
}
But the page shows error An error occurred : Unable to load the requested class: mymenu. Why this error is showing class name as mymenu(all in lowercase) wherein I wrote myMenuat controller
但是页面显示错误An error occurred : Unable to load the requested class: mymenu。为什么此错误将类名显示为 mymenu(全部为小写),其中我myMenu在控制器中写道
回答by ashiina
Two problems:
两个问题:
1) Your naming convention is wrong.
1)您的命名约定是错误的。
In CodeIgniter, libraries must start with a capitalized letter. The class name and file name both have to start with capital letters, and they have to match. Refer to the document below. https://www.codeigniter.com/user_guide/general/creating_libraries.html
在 CodeIgniter 中,库必须以大写字母开头。类名和文件名都必须以大写字母开头,并且必须匹配。请参阅下面的文档。 https://www.codeigniter.com/user_guide/general/creating_libraries.html
2) You shouldn't instantiate myMenu with new.
When accessing a library which you loaded, this is pretty much the usual way:
2) 你不应该用new. 访问您加载的库时,这几乎是通常的方式:
$this->load->library('mymenu'); // when calling the loader, the case doesn't matter
$data['menu'] = $this->mymenu->show_menu(); //'mymenu' is the lowercase of the class name
回答by Willem Ellis
Class name should be capitalized. class MyMenu
类名应该大写。 class MyMenu

