CodeIgniter2.2.0-在控制器里调用load失败报错的问题
报错如下:
1 2 3 4 5 6 7 8 9 10 11 12 | hello A PHP Error was encountered Severity: Notice Message: Undefined property: Test:: $load Filename: controllers/test.php Line Number: 9 Fatal error: Call to a member function view() on a non-object in D:\xampp\htdocs\citest\application\controllers\test.php on line 9 |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php if (!defined( 'BASEPATH' )) exit ( 'No direct script access allowed' ); class Test extends CI_Controller{ public function test() {<br> //此处是引发错误的根源 echo 'hello' ; } public function index() { $this ->load->view( 'test/index' ); } public function about() { $this ->load->view( 'test/about' ); } protected function test1() { echo 'test protected function' ; } private function hello() { echo 'hello,ci' ; } public function test2() { $this ->test1(); echo '<br/>' ; $this ->hello(); } } |
看到哪里错了吗?因为我重写了test控制器的构造函数,这和类名一样的public方法和__construct方法是一样的功能的,重写了之后CI_Controller父类里的实例化什么的都没了,必须自己实例化了。
除了把和类同名的方法移除外,还有中方法如下:
1 2 3 4 5 6 7 8 9 | public function test() { echo 'hello' ; parent::__construct(); } public function index() { $this ->load->view( 'test/index' ); } |
这样就没问题了。
时隔三年,重新使用codeigniter,忘却了许多事情。。。
-------------------------------------------------
保持专注,只做一件事,做好这件事!@
-------------------------------------------------
分类:
CodeIgniter
标签:
codeigniter
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
· 记一次 .NET某固高运动卡测试 卡慢分析
· 微服务架构学习与思考:微服务拆分的原则
· 记一次 .NET某云HIS系统 CPU爆高分析
· 如果单表数据量大,只能考虑分库分表吗?
· 7 个最近很火的开源项目「GitHub 热点速览」
· DeepSeekV3:写代码很强了
· Visual Studio 2022 v17.13新版发布:强化稳定性和安全,助力 .NET 开发提
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
· 让 AI 对接 MySQL 数据库实现快速问答对话