类的继承,为什么会报错啊?请大家来研究研究?
<?
class MYSQL{
function MYSQL($host,$user,$password,$db){ //构造类的属性
$this->host=$host;
$this->user=$user;
$this->password=$password;
$this->db=$db;
$this->query=0;
$this->num=0;
$this->result=0;
}
function conn(){ //连接数据库服务器
$this->conn=@ mysql_connect($this->host,$this->user,$this->password);
if(!$this->conn) return false;
else return $this->conn;
}
function db(){ //选择数据库
if(!@ mysql_select_db($this->db,$this->conn)) return false;
else return true;
}
function query($sql){ //执行Sql命令
$this->query=@ mysql_query($sql);
if(!$this->query) return false;
else return $this->query;
}
function num(){ //查寻数据库记录数
$this->num=@ mysql_num_rows($this->query);
if(!$this->num) return false;
else return $this->num;
}
function result(){ //取得执行Sql后的结果数组
$this->result=@ mysql_fetch_array($this->query);
return $this->result;
}
function free(){ //释放结果集合使用的内存
@ mysql_free_result($this->query);
}
function close(){ //关闭库
@ mysql_close($this->conn);
}
}
class SYSINFO extends MYSQL {
function sysname($condition){
$this->conn();
$this->db();
$sql="select * from `sysinfo`";
$this->query($sql);
$result=$this->result();
switch($condition){
case 0:
echo $result[sysname];
break;
case 1:
echo $result[sys_version];
break;
case 2:
echo $result[sysadminemail];
break;
case 3:
echo $result[create_time];
break;
}
}
}
?>
当调用子类sysinfo的时候就会包以下错误,但程序还是能得到正确的结果的,其中"V1.0"就是想要的结果.
类是这样使的:
<?
require_once("conn.php");
require_once("include/class.inc");
$sysinfo=new SYSINFO();
$sysinfo->sysname("1");
?>
小弟我实在找不到原因了,特请大侠们相助!
Warning: Missing argument 1 for MYSQL::MYSQL() in /home/handong/web/dhmf/include/class.inc on line 4
Warning: Missing argument 2 for MYSQL::MYSQL() in /home/handong/web/dhmf/include/class.inc on line 4
Warning: Missing argument 3 for MYSQL::MYSQL() in /home/handong/web/dhmf/include/class.inc on line 4
Warning: Missing argument 4 for MYSQL::MYSQL() in /home/handong/web/dhmf/include/class.inc on line 4
V1.0
问题点数:20、回复次数:12Top
1 楼twt326(天地小子)回复于 2004-12-01 23:19:41 得分 5
意思是你的第 1 2 3 4 个参数没有。。。
$sysinfo=new SYSINFO($host,$user,$password,$db);
这里应该加入四个参数,
因为你的类构造函数需要参数,而你又没有设置黙认值 。。。
如果你不想加的话就将
function MYSQL($host,$user,$password,$db)
{。。。}
改成
function MYSQL($host="xxx",$user="xxx",$password="xxx",$db="xx")
{。。。}Top
2 楼ice_berg16(寻梦的稻草人)回复于 2004-12-01 23:21:56 得分 5
你都没有指定那些参数,
由于你的子类没有构造函数,它就会执行父类的构造函数,
而父类的构造函数是需要参数的,你没有提供,所以出错了
你应该在子类指定构造函数
function SYSINFO($host,$user,$password,$db)
{
parent::MYSQL($host,$user,$password,$db);
}Top
3 楼spacet(空格t)回复于 2004-12-01 23:42:59 得分 0
恩,看手册上面关于类的继承就知道了,当子类没有构造函数的时候,执行父类构造函数Top
4 楼handong2004(冬雨)回复于 2004-12-02 00:09:25 得分 0
class CHECKONLINE extends MYSQL {
//更新在线人数
function checkonline(){
$this->conn();
$this->db();
$sql="select * from `online`";
$this->query($sql);
while($result=$this->result()){
if(($result[logintime]+300)<time()){
$userid=$result[userid];
$sqldeluserid="delete from `online` where userid='$userid'";
$this->query($sqldeluserid);
}
}
}
那么这个子类为什么没有问题呢?已经测试过没有任何报错信息Top
5 楼surfchen(冲浪)回复于 2004-12-02 00:13:06 得分 0
那么这个子类为什么没有问题呢?已经测试过没有任何报错信息
================
因为这个子类有了构造函数Top
6 楼handong2004(冬雨)回复于 2004-12-02 00:16:46 得分 0
哪有构造函数阿?这个子类里不就一个自定义函数!Top
7 楼hqs_bb(热衷者)回复于 2004-12-02 05:31:50 得分 0
你用的自定义函数名称与子类名相同,不就是构造函数吗?(大小写不敏感的)Top
8 楼skystar008(疯花血月)回复于 2004-12-02 07:55:47 得分 0
MYSQL($host,$user,$password,$db)
下面有参数呀,如果你下面没参数就要有构造函数.Top
9 楼surfchen(冲浪)回复于 2004-12-02 10:50:28 得分 0
类同名的函数也是构造函数Top
10 楼fzjw(冰凌尘埃)回复于 2004-12-02 11:24:39 得分 5
PHP中构造函数是和类名称相同的函数
比如:
<?php
class db_mysql{
function db_mysql($host,$user,$passwd,...){
......
}
function ...
}
?>
这在PHP4中很常见,PHP5支持这么写,但是有了新的写法
class db_mysql{
public function __construct(...){ //__construct是默认的构造函数
......
}
public function __destruct(){ //__destruct是默认的析构函数
......
}
}
还有克隆等默认方法Top
11 楼WindyWebEx(Windy_WebEx)回复于 2004-12-02 13:41:45 得分 5
子类继承于父类,如果你的子类有构造函数,那么就会覆盖父类的构造函数,如果没有,那么就会沿用父类的构造函数。
父类中的构造函数需要有参数,而你没有提供,所以出错Top
12 楼handong2004(冬雨)回复于 2004-12-03 00:08:09 得分 0
ok 解决了。发分Top




