A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearPHP - What should I call the information stored in my Page class? - page 1
User InfoPosts
PHP - What should I call the information stored in my Page class?#1
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

posted date: 2008-12-17 16:37:00


Re: PHP - What should I call the information stored in my Page class?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-17 16:37:01


Re: PHP - What should I call the information stored in my Page class?#3
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.

posted date: 2008-12-17 16:48:00


Re: PHP - What should I call the information stored in my Page class?#4
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).

posted date: 2008-12-17 16:49:00


Re: PHP - What should I call the information stored in my Page class?#5
When you say messier, do you mean that it's easier to lose what the array contains?

posted date: 2008-12-17 16:56:00


Re: PHP - What should I call the information stored in my Page class?#6
we prefer the term "stackheads"

posted date: 2008-12-17 17:03:00


Re: PHP - What should I call the information stored in my Page class?#7
@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'").

posted date: 2008-12-17 17:07:00


Re: PHP - What should I call the information stored in my Page class?#8
I thought the official term was 'stackers' I read somewhere, on a so-faq tagged question.

posted date: 2008-12-17 17:24:00


Re: PHP - What should I call the information stored in my Page class?#9
If I was going to use all getters to pull data, would it be more acceptable to use an array?

posted date: 2008-12-17 17:26:00


Re: PHP - What should I call the information stored in my Page class?#10
you might have - I could be wrong on that

posted date: 2008-12-17 17:29:00


Re: PHP - What should I call the information stored in my Page class?#11
@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.

posted date: 2008-12-17 18:02:00


Re: PHP - What should I call the information stored in my Page class?#12
I've updated my post with an example using __get. Hope this helps!

posted date: 2008-12-17 18:09:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.