MasterACE14 Posted June 30, 2010 Share Posted June 30, 2010 Good day, I've written a basic class to simply get the latest news from a MySQL table, and through the use of public methods, each column's data can be displayed. However the $row[] array is coming up empty. I suspect I'm not using the constructor properly. <?php class News { function __construct() { $row = $this->getNews("SELECT * FROM `ca_news` ORDER BY `posttime` DESC"); // this isn't right? } private function getNews($sql) { global $database; $q = $database->query($sql); $row = $database->fetch_array($q); return $row; } public function newsDate($format) { global $row; return date($format, strtotime($row['posttime'])); } public function newsAuthor() { global $row; return $row['author']; } public function newsText() { global $validate; global $row; return $validate->cleanInput( "alphanum", $row['text'] ); } } ?> <?php $news = new News; ?> <div style=""> <center> <table class="art-article" border="0" cellspacing="0" cellpadding="0" style="width: 80%; text-align: center;"> <th>News</th> <tbody> <tr> <th><?php echo $news->newsAuthor(); ?></th> <th><?php echo $news->newsDate("F j, Y"); ?></th> </tr> <tr> <td colspan="2"><?php echo $news->newsText(); ?></td> </tr> </tbody></table> </center> </div> appreciate the help, thanks Quote Link to comment https://forums.phpfreaks.com/topic/206246-__construct-not-used-correctly/ Share on other sites More sharing options...
trq Posted June 30, 2010 Share Posted June 30, 2010 I suspect I'm not using the constructor properly Your not using the entire concept of a class properly. Don't use globals within class methods to pass data around. Your missing the entire point of classes. You should be using private properties to store data belonging to a class. This data is the accessible to all methods within said class. Quote Link to comment https://forums.phpfreaks.com/topic/206246-__construct-not-used-correctly/#findComment-1079005 Share on other sites More sharing options...
MasterACE14 Posted June 30, 2010 Author Share Posted June 30, 2010 Don't use globals within class methods to pass data around. Your missing the entire point of classes. You should be using private properties to store data belonging to a class. This data is the accessible to all methods within said class. I have the following now and it's working, is this the general idea? class News { private $row; function __construct() { $this->row = $this->getNews("SELECT * FROM `ca_news` ORDER BY `posttime` DESC"); } private function getNews($sql) { global $database; $q = $database->query($sql); $row = $database->fetch_array($q); return $row; } public function newsDate($format) { return date($format, strtotime($this->row['posttime'])); } public function newsAuthor() { return $this->row['author']; } public function newsText() { global $validate; return $validate->cleanInput( "alphanum", $this->row['text'] ); } } Quote Link to comment https://forums.phpfreaks.com/topic/206246-__construct-not-used-correctly/#findComment-1079018 Share on other sites More sharing options...
trq Posted June 30, 2010 Share Posted June 30, 2010 Its better but $database and $validate appear from nowhere. this completely breaks the entire idea of encapsulation. Quote Link to comment https://forums.phpfreaks.com/topic/206246-__construct-not-used-correctly/#findComment-1079023 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.