<?php 
class DB

var $dbserver; //数据库服务器
var $dbuser;//数据库用户名
var $dbpass; //数据库密码
var $dbname; //数据库名
function __construct($dbserver,$dbuser,$dbpass,$dbname){
$this->dbserver=$dbserver;
$this->dbuser=$dbuser;
$this->dbpass=$dbpass;
$this->dbname=$dbname;
}
function connect_DB(){
$link=mysql_connect($this->dbserver,$this->dbuser,$this->dbpass);
if(!$link){
return false;
}else{
return $link;
}
}
function colse_DB($link){
if(mysql_close($link)){
return true;
}else{
return false;
}
}
function queryRecord($cmd,$dbtable){
$link=$this->connect_DB();
mysql_select_db($this->dbname, $link) or die ('Can\'t use db : ' . mysql_error());
$result=mysql_query($cmd);
if($result){
$lastarr=array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$arr=array($array);
$lastarr=array_merge($lastarr,$arr);
}
$this->colse_DB($link);
return $lastarr;
}else{
return false;
}
}
function updateRecord($cmd,$dbtable){
$link=$this->connect_DB();
mysql_select_db($this->dbname, $link) or die ('Can\'t use db : ' . mysql_error());
$result=mysql_query($cmd);
if($result){
return true;
}else{
return false;
}
}
}
<?php
class News extends  DB{
//数据表的名称
var $dbtable;
function __construct($dbtable){
$this->dbtable = $dbtable;
}
/**
 * 新闻的添加
 * @param array(id->"" title->"" content->"")
 * @return boolean
 */
function addNews($array){
$cmd="select MAX(id) from $this->db_table";
//print_r($cmd);
$Maxid=$this->queryRecord($cmd,$this->dbtable);
print_r($Maxid);
$id=$Maxid[id]+1;
echo $id;
//传进来的参数;
print_r($array);
$cmd="insert into $this->db_table(id,title,content)values('$id','$array[title]','$array[content]')";
print_r($cmd);
if($this->updateRecord($cmd,$this->dbtable)){
return true;
}else{
return false;
}
}
/**
 * 获取类别新闻
 * @param $cate_id
 * @return array
 */
function getNewsByCate_id($cate_id){
$cmd="select * form $this->db_table where cate_id=$cate_id";
//print_r($cmd);
$array=$this->queryRecord($cmd,$this->dbtable);
//print_r($array);
}
}
/*
$News = new News("text","news");
$array = array("title"=>"测试用新闻标题","测试用新闻内容");
$user->addNews($array);
*/
?>
我要提的问题是:调用php子类构造函数,之前是否会调用父类的构造函数require './inc/conn.inc.php';
require './inc/DB.class.php';
require './inc/News.class.php';
//echo $dbserver;
$news = new News('news');
echo $news->dbtable;
echo $news->dbserver;我测试的怎么没有调用。为什么

解决方案 »

  1.   

    不会~自己用 parent 调用
      

  2.   

    你的子类里重写了__construct
    so,父类那个失效了~
      

  3.   

    <?php
    class A{
    function __construct($a,$b,$c){
    echo "I'm A";
    if(func_num_args()>3){
    $this -> API(func_get_arg(func_num_args()-1));
    }
    }

    function API(){}
    }class B extends A{
    function API($d){
    echo "I'm B".$d;
    }
    }$a = new A("","","");
    echo "<br/>";
    $b = new B("","","",",Hi Cay!");?>
    这应该就是你想要的,其实就是重载一个方法,20分~貌似少了点...建议追加~哈哈
      

  4.   

    问题已解决,,在子类构造函数中,写上parent::__construct();谢谢楼上。虽然没有给你加分