dtsps2098
dtsps2098
2013-12-30 12:01

如何在子类中调用基类的构造函数

  • oop
  • php

Below is my base class i.e database methods.

// Constructor
public function __construct($argHost, $argUsername, $argPassword, $argDatabase)
{
    $this->_host = $argHost;
    $this->_username = $argUsername;
    $this->_password = $argPassword;
    $this->_database = $argDatabase;
}

// Connect to the database
public function Connect()
{
    if (!$this->Is_Connected()) {
        $this->_connection = mysqli_connect($this->_host,$this->_username,$this->_password,$this->_database);    
    } else {
        return $this->_connection;
    }

}
// Run query
public function Run($query)
{
    if ($this->result = mysqli_query($this->_connection,$query)) {
        return $this->result;
    } else {
        die("Couldn't perform the request");
    }
}

And my child class is categories method below

class Categories extends Database
{    
    public $category_id = '';
    public $category_name = '';
    public $category_image = '';

    // View Category
    public function ViewCategories() 
    {
        return $this->Run("SELECT * FROM categories");
    }       
}

Now the problem is that when i am running the Run() method by creating object of the base class it is working fine. But when i am creating object object the child class i.e categories and executing the method viewCategories(); i m receiving below errors

Warning: Missing argument 1 for Database::__construct(), called in E:\xampplite\htdocs\ecommerce\index.php on line 16 and defined in E:\xampplite\htdocs\ecommerce\classes\class.database.php on line 17

Warning: Missing argument 2 for Database::__construct(), called in E:\xampplite\htdocs\ecommerce\index.php on line 16 and defined in E:\xampplite\htdocs\ecommerce\classes\class.database.php on line 17

Warning: Missing argument 3 for Database::__construct(), called in E:\xampplite\htdocs\ecommerce\index.php on line 16 and defined in E:\xampplite\htdocs\ecommerce\classes\class.database.php on line 17

Warning: Missing argument 4 for Database::__construct(), called in E:\xampplite\htdocs\ecommerce\index.php on line 16 and defined in E:\xampplite\htdocs\ecommerce\classes\class.database.php on line 17

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in E:\xampplite\htdocs\ecommerce\classes\class.database.php on line 35 Couldn't perform the request

Udated : This is how i am calling the methods

<?php
function __autoload($class_name) {
    include 'classes/class.'.$class_name . '.php';
}
$connection = new Database("localhost", "raheel", "raheel786", "ecommerce");
$connection->Connect();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ecommerce</title>
</head>
<body>
   <?php 
   $category = new Categories();
   $category_list = $category->ViewCategories();
   var_dump($category_list);
   ?> 
</body>
</html>

Kindly help me how to fix that.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答