【问题标题】:difference between index, construct and class name functions in codeigniter classcodeigniter 类中索引、构造和类名函数之间的区别
【发布时间】:2011-02-21 20:33:43
【问题描述】:
在使用 Codeigniter 之后,我仍然无法弄清楚这 3 个函数之间的区别。是不是所有的函数都是调用类自动调用的?
class Upload extends Controller {
function Upload()
{
parent::Controller();
echo 'test';
}
function __construct()
{
parent::Controller();
echo 'test';
}
function index()
{
echo 'test';
}
}
【问题讨论】:
标签:
php
oop
class
codeigniter
【解决方案1】:
函数 Upload() 是 PHP4 的东西。这是 Upload 对象的构造函数,已弃用。
__construct() 是构造函数的“新”方式
index() 在 index 动作上被调用,这是默认动作
访问 /uploads 或 /uploads/index 将运行此功能。其他两个函数将始终运行。
希望这能解决问题!
【解决方案2】:
您确实需要从空白屏幕重新开始,并阅读Codeigniter Controllers 上的文档。
并确保您使用的是 CI 2.0
编辑版本(针对 CI 2.0 进行了更正)
<?
class Upload extends CI_Controller
{
function __construct()
{
parent::__construct();
echo 'test';
}
function index()
{
echo 'test';
}
}
每次加载控制器时都会调用__construct()
index() 是在 uri 中没有给出函数时调用的默认函数
例如。 localhost/index.php/upload 实际上会调用localhost/index.php/upload/index/