Jump to content

__construct not used correctly?


MasterACE14

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/206246-__construct-not-used-correctly/
Share on other sites

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.

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'] );
    }

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.