hahacc 发表于 2009-4-11 10:39:59

自定义类构造函数提示参数缺失的奇怪问题

自定义一个类(system/application/Myclass.php):
class Myclass{
function Myclass($file,$ext){
    ....
}
}

然后在某个Controller里调用:
$this->load->library('myclass');
$this->myclass->myclass('a','b');

可是却提示:
A PHP Error was encountered
Severity: Warning

Message: Missing argument 1 for Down::Down(), called in C:\xampp\htdocs\rmbweb\rmbweb\system\libraries\Loader.php on line 931 and

defined

Filename: libraries/Down.php

Line Number: 3

但是,奇怪的问题出现了:当把Myclass类中的构造函数去掉,而直接:
class Myclass{
function hello($file,$ext){
    ....
}
}
然后调用:
$this->load->library('myclass');
$this->myclass->hello('a','b');
却没有出现错误。
请问这是怎么回事呢?

yuwen002 发表于 2009-4-11 11:28:09

不对了。你根本没理解构造函数的意义。。
他是类初始化时调用的函数
$this->load->library('myclass');
已经初始化了。
要在$this->load->library('myclass', $params);
把构造函数的参数传进去。
具体的自己看一下帮助。那里已经有详细说明了。

yuwen002 发表于 2009-4-11 11:33:33

http://codeigniter.org.cn/user_guide/general/creating_libraries.html

hahacc 发表于 2009-4-11 12:01:29

2# yuwen002
你好聪明啊,我咋就没想到呢?
感觉自己的PHP基础太差了。
Thank you so much.
页: [1]
查看完整版本: 自定义类构造函数提示参数缺失的奇怪问题