Calling a variable set in a class from outside that class

I have a class for news item. In one of the functions within that class I have set a variable:


$news_id = $row['news_id'];

On the main news page (news.php) I call the class and try to call the variable:


    require('cNews.php');
   $cNews = new cNews($pdo);
   $news_id = $cNews->get_news($news_id);

But this is obviously not the right syntax, because I need the id for an update but the update is not happening. What is the richt syntax to call a varianle that was set within a class outside of that class?

Thank you all

Hi Donboe,

I’m not entirely sure what you mean. In your example you show $news_id being passed into the cNews::get_news() method, but when you say that the variable is set within the class it’s a little confusing. Where does the value for $news_id ultimately come from?

Hi fretburner

I have a class called cNews. Within that class I have a function called get_news. Within that function I have the following:


    $isNew       = 1;
    $chkN = "SELECT news_id FROM news WHERE $isNew = ?";
    $chkNew = $this->pdo->prepare($chkN);
    $chkNew->execute(array($isNew));
    $row = $chkNew->fetch(PDO::FETCH_ASSOC);
    $news_id = $row['news_id'];// this is the variable

OK, so you basically just want to return $news_id from your method, so you can use it elsewhere?

Note that you don’t need to use prepared statements if you are always going to be executing the same query (i.e. you’re always going to be querying for records where isNew is 1):


public function get_news()
{
    $sql = "SELECT news_id FROM news WHERE isNew = 1";
    $result = $this->pdo->query($sql);
    $row = $result->fetch(PDO::FETCH_ASSOC);

    return $row['news_id'];
}

Hi fretburner. Thanks again for the tips and advise. I have 2 last questions though. How do I use $row[‘news_id’]; elsewere on other pages for example in an update query.


$u = "UPDATE news SET isNew = 0  WHERE news_id = ?";

and the second question. What if I need return not just news_id, but other values as well? Could I do it like this:


return $row['news_id'];
return $row['news_title'];

Thank you in advance!

If you need the news_id on other pages, you could call the get_news() method again on those pages. Alternatively you might decide that your get_news() function should automatically update the table whenever unread news is retrieved.

No, that wouldn’t work, you can only return a single value from a function/method in PHP. The value that you return can, however, be an array or object, so you could just return the entire $row array.

Hi fretburner I realy need to return more then 1 value, so how does such an array look like an how do I get the different values when I need them on different places?

Instead of returning $row[‘news_title’], you can return $row from the method, then you have access to all the columns from your DB query.

Rather than returning individual values such as a title or an id, think about a row from your news table as an object. Ideally you’d have a separate class to represent a ‘news item’, but for now (as a step in the right direction) you could just ask PDO to return rows as objects.

Here’s an example of a simple data access object (DAO) class for news. It’s probably similar to your cNews class in some ways. It has a getById method which will return an object representing a row from the news table, containing all the table columns


<?php

class NewsDAO {

	protected $pdo;

	public function __construct(PDO $pdo)
	{
		$this->pdo = $pdo;
	}

	public function getById($newsId) 
	{ 
	    $sql  = "SELECT * FROM news WHERE news_id = :id"; 
	    $stmt = $this->pdo->prepare($sql); 
	    $stmt->execute(array(':id' => $newsId));

	    return $stmt->fetch(PDO::FETCH_OBJ);
	}

	public function getUnread() 
	{ 
	    $sql    = "SELECT news_id FROM news WHERE isNew = 1";  
	    $result = $this->pdo->query($sql);

	    return $result->fetchAll(PDO::FETCH_OBJ);
	}

	// Other methods for creating and updating news..

}

To display all the unread news, you’d do something like this:


$unreadNews = $news->getUnread();

foreach ($unreadNews as $newsItem) {
    echo 'ID: ' . $newsItem->news_id;
    echo 'Title: ' . $newsItem->news_title;
    // etc.
}

This is in one word brilliant fretburner. Thanks a lot