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
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.

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.