tags:

views:

182

answers:

2

Hi Stackers

I have a class named Page with a private property currently called _pageData which stores all the information (such as title, content, keywords etc).

However, this to me doesn't look so good when I refer to it $this->_pageData. I want to think of a better name, and I'd imagine there would probably be a standard or 'best practise' name/prefix/suffix for these sorts of things. I thought about '_assets'.

May I ask what you have used in the past, an unambiguous name that hasn't made you down the track add some data to it and go "Oh no, now my array has data in it that's outside the scope of the variable name". This has happened to me a few times so I'd like to see what has been proven to work.

Thank you

A: 

That's very specific and I don't think there is a "standard" or "best practice". Just call it whatever it feels best in your opinion. In my own, I would simply call it "data" as Page is the class, $this = Page, so Page->Data. This would suffice for me.

I can't think of a better name... Maybe it sounds weird because you use the _ prefix? I don't like to use those kinds of prefixes, specially in PHP, it doesn't make much sense, but that's just me.

Nazgulled
+2  A: 
class Page {
    public $title = '';
    public $keywords = array();
    public $content = '';
    // etc.
}

$page = new Page();
echo '<title>' . $page->title . '</title>';
echo $page->content;

Or you can use accessors/get-set and the like to protect your data, allow it to be modified with persistence, or whatever. This allows for lazy initialization and lazy writing (not sure if there's a proper term for the latter). Example:

class Page {
    private $data = array('title' => '', 'keywords' => array(), 'content' => '');

    public __get($name) {
        if(isset($this->data[$name]))
            return $this->data[$name];

        /* You probably want to throw some sort of PHP error here. */
        return 'ERROR!';
    }
}

$page = new Page();
echo '<title>' . $page->title . '</title>';
echo $page->content;

(See overloading in the PHP5 manual for more details.)

Note you can hide $data members or even modify them or add new ones ($page->contentHTML could transform markdown to HTML, for example).

Using the name _pageData is redundant for a Page class. You already know it's a page, so you're repeating information ($currentPage->_pageData vs. $currentPage->data).

I also find associative arrays a little messier for this kind of thing, but they may be needed if you want a really dynamic system. Regardless, you can implement your template system to access class members by name ($member = 'title'; echo $page->$member; // echoes $page->title), assuming this is what you wanted the array for (other than an easy database query, which you can use list() for).

strager
When you say messier, do you mean that it's easier to lose what the array contains?
alex
@alex, Yes, and the syntax looks less OO-like and less safe. Also, $page->title is shorter than $page->data['title'], and is much cleaner to read ("Page's title" vs. "Page's data item 'title'").
strager
If I was going to use all getters to pull data, would it be more acceptable to use an array?
alex
@alex, What do you mean, use getters? $page->getTile() is cleaner than $page->data()['title'] (or does PHP not support that? Then: $data = $page->data(); $data['title']). You can also use __get (I think it's called that) and keep an internal array.
strager
I've updated my post with an example using __get. Hope this helps!
strager