简体   繁体   中英

PHP class, extends database not working

I have a PHP class that extends another class, but i only get the MySQL to work at the extended class not the first class. Anyone knows what the problem can be? I can't seem to figure it out at all right now :S

# Vote class.
class vote { 
    public $newsID;
    private $db;

    # Construct.
    public function __construct() { 
        global $_database;
        $this->db = $_database; 
    }

    # Vote Up. 
    public function voteUp() { 
        return '<a href="#" class="fa fa-angle-up" style="position:absolute;top: 1px; right: 10px;"></a>'; 
    }
    public function voteScore($newsID) {

        $vote = mysqli_fetch_object($this->db->query("SELECT * FROM ".PREFIX."news WHERE newsID='".$newsID."' LIMIT 1"))->vote;
        return '<span class="BigFontSize" style="position:absolute; top: 37px;right: 14px;">'.$vote.'</span>'; 
    }
    public function voteDown() { 
        return '<a href="#" class="fa fa-angle-down" style="position:absolute; bottom: 0;right: 10px;"></a>'; 
    }
}

# News class.
class news extends vote { 
    public $countNews; 
    private $db;

    # Construct.
    public function __construct() { 
        global $_database;
        $this->db = $_database; 
    }

    # Count News.
    public function countNews() { 
        return $this->db->query("SELECT * FROM ".PREFIX."news ORDER BY date DESC")->num_rows; 
    }


    # Print the news.
    public function GetNews() {
        $newsArray = array();
        $sql = $this->db->query("SELECT * FROM ".PREFIX."news ORDER BY date DESC"); 
        while ($rad = $sql->fetch_array()) { 
            $newsArray[] = array('headline' => $rad['headline'], 'content' => $rad['content'], 'date' => $rad['date'], 'poster' => $rad['userID'], 'published' => $rad['published'], 'intern' => $rad['intern'], 'newsID' => $rad['newsID']);
        }
        return $newsArray;
    }
}

It's the vote class that doesnt have the functioning database. Am i missing something?

Just remove the constructor from news and it will inherit the vote constructor. You will have to make the $db class var in vote protected instead of private and remove it from news altogether. There is absolutely no gain from each having it's own reference to the same $database since it will still be the same instance.

While we are on the subject of better code design, do not use GLOBAL in PHP, EVER. PHP global in functions http://smartik.ws/2014/07/do-not-use-php-global-variables-never/

Instead of accessing a global connection object in the constructor as you are doing, use Dependency Injection, ie pass it into the constructor as a parameter:

# Vote class.
class vote { 
    public $newsID;
    private $db;

    # Construct.
    public function __construct($_database) 
    { 
        $this->db = $_database; 
    }
}

$_database = new Database();
$news = new News($_database);

In the long term you will find that this is the prefered practice for very good reasons. It will also mark you out as a professional not an amateur. http://tutorials.jenkov.com/dependency-injection/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM