-1

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?

Tommy
  • 667
  • 1
  • 10
  • 25
  • What is the error you're getting? – q.Then Oct 12 '15 at 15:58
  • side note: Despite one might start to think so in a world of twitter and reddit a news is not a vote. A news article might be about a vote or there might a vote/poll associated with news but `news extends vote` ....that doesn't sound right. – VolkerK Oct 12 '15 at 16:03
  • show us the code fragment where you instantiate your `news` object and have any issues or error message? – Alex Oct 12 '15 at 16:06

1 Answers1

1

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

Community
  • 1
  • 1
David Soussan
  • 2,698
  • 1
  • 16
  • 19
  • Yes, but this will be a really bad idea if the news class need to initialise something too. We dont know that. I agree that the setup is a little bit weird, but the correct answer to that is to reuse parent $this->db, not to skip the child constructor. The problem _is_ that the parents constructor never is initialised. – davidkonrad Oct 12 '15 at 16:17
  • 1
    If and when the child class needs to do something new then is the time to add its own constructor. That really is the whole point of inheritance. Even then it would not need it's own class var for the database object, you would call parent::constructor and then add the new code. – David Soussan Oct 12 '15 at 16:27
  • You _do_ realize that `$_database` is a global variable that have nothing to do what so ever with those two class implementations? It is a simple reference, there is nothing bad in reusing `$_database` - in fact, "_that is really the whole point of having global variables_". You are mixing things together and ask OP to change his class-member visibility setup where he simply just need to call the ancestor constructor. – davidkonrad Oct 12 '15 at 16:36