Is fetching data from database a get-method thing?

Posted by theva on Stack Overflow See other posts from Stack Overflow or by theva
Published on 2013-10-27T09:48:56Z Indexed on 2013/10/27 9:53 UTC
Read the original article Hit count: 123

Filed under:
|
|

I have a small class that I call Viewer. This class is supposed to view the proper layout of each page or something like that...

I have a method called getFirstPage, when called the user of this method will get a setting value for which page is currently set as the first page. I have some code here, I think it works but I am not really shure that I have done it the right way:

class Viewer {

private $db;
private $user;
private $firstPage;

function __construct($db, $user) {

    $this->db = $db;

    if(isset($user)) {
        $this->user = $user;
    } else {
        $this->user = 'default';
    }
}

function getFistPage() {
    $std = $db->prepare("SELECT firstPage FROM settings WHERE user = ':user'");
    $std->execute(array(':user' => $user));
    $result = $std->fetch();
    $this->firstPage = $result['firstPage'];

    return $this->firstPage;
}
}

My get method is fetching the setting from databse (so far so good?). The problem is that then I have to use this get method to set the private variable firstPage. It seems like I should have a set method to do this, but I cannot really have a set method that just fetch some setting from database, right? Because the user of this object should be able to assume that there already is a setting defined in the object...

How should I do that?

© Stack Overflow or respective owner

Related posts about php

Related posts about database