dongzhuji1042 2011-08-02 21:53
浏览 43
已采纳

调用扩展类方法问题

I have a primary class and an extended one (database connections).

Parent Classname Classica
Extended DatabaseQ

How could I call the extended class methods within parent?

This is not working:

$this->connectdb();

or this:

$this->DatabaseQ->connectdb();

Example Code:

Extended

class DatabaseQ extends Classica{

    public $dbhost;
    public $dbname;
    public $dbuser;
    public $dbpass;

    function __construct(){
        include('config.php');
        $this->dbhost = $dbhost;
        $this->dbname = $dbname;
        $this->dbuser = $dbuser;
        $this->dbpass = $dbpass;
    }

    #connect to database
    public function connectdb(){      
        $link = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass);
        if (!$link) {
          die('Could not connect: ' . mysql_error());
        }else {
            //echo 'Connected Successfully to Database<br>';
        }
        @mysql_select_db($this->dbname) or die( "Unable to select database!");
    } 

    #read database
    function readdb(){        
    }    
    #update database
    private function updatedb(){        
    }
    #close database connection
    function closedb(){
        mysql_close();    
    }

}

Parent

class Classica{  

        function sample_method(){
            //connect db here
            //run some sql queries here
        }
  • 写回答

2条回答 默认 最新

  • dongmi5020 2011-08-02 23:30
    关注

    Judging by the contents of both of your classes there is absolutely no reason for you to make any use of the Classica class at all because you are unnecessarily spreading functional responsibility over several classes when you could have it all wrapped up inside a single class.

    I see that the only reason why you are using the "parent" class is to connect to your database and do some initial queries. Unless you plan to implement some advanced design patterns later there is absolutely no reason why you could not do this inside the DatabaseQ constructor.

    class DatabaseQ {
        public $dbhost;
        public $dbname;
        public $dbuser;
        public $dbpass;
    
        function __construct(){
            include('config.php');
            $this->dbhost = $dbhost;
            $this->dbname = $dbname;
            $this->dbuser = $dbuser;
            $this->dbpass = $dbpass;
    
            $this->connectdb(); // This is a good place to initiate your DB connection
            $this->doOtherInitStuff(); // Calling the rest of the init stuff.
        }
    
        /**
         * This is the place where you do all of your init stuff. 
         * Note the private status! The environment doesn't need to have access to your DB initialization stuff
         */
        private function doOtherInitStuff() {
            // Do init stuff
        }
    
        #connect to database
        private function connectdb(){ // Note the private scope too! Only the object itself needs to know how to connect to the db!
            $link = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass);
            if (!$link) {
              die('Could not connect: ' . mysql_error());
            }else {
                //echo 'Connected Successfully to Database<br>';
            }
            @mysql_select_db($this->dbname) or die( "Unable to select database!");
        } 
    
        #read database
        function readdb(){        
        }    
        #update database
        private function updatedb(){        
        }
        #close database connection
        function closedb(){
            mysql_close();    
        }
    
    }
    

    On the other hand if you intend to create a base class which will be used later for let say different DB "drivers" (extended classes with overloaded methods) you could create an abstract class which will only contain the blueprint for all the methods which your extended (driver) classes would need to implement.

    But that's a bit advanced story :)

    EDIT: If you need a class which would specifically be used for outputting stuff which DatabaseQ retrieves then create an extended class of DatabaseQ and put inside of it everything that will spit data out.

    class DatabaseQOutput extends DatabaseQ {
        public function __construct(){
            parent::__construct(); // You make sure here that the parents constructor is executed and a DB connection and initialization stuff is taken care off 
        }
    
        public function output() {
    
        }
    }
    
    $db = new DatabaseQOutput();
    $db->output();
    

    But to tell you the truth you don't actually want any of your database specific classes to be responsible for outputting data because generally that's not their job. Database classes should be considered as models although you are not using MVC which means their role is primarily to serve as an abstraction layer for databases and all data fetching / sending operations.

    If I were you I would create a class which is specifically tasked with outputting data which is retrieved with your database classes. That way you would create a class which is acting as a view in a way and will accept all responsibility for outputting data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大