TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
View Poll Results: Do you use public member variables or get functions to retrieve data from classes?
Public member variables 1 12.50%
Individual get functions 3 37.50%
Implement __call method where necessary with individual get functions 2 25.00%
Get functions when arguments required, else public member variables 2 25.00%
Voters: 8. You may not vote on this poll

 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 06-08-2009, 01:42 AM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Images How do you access your data in a class?

I am curious because up-to-now I have always been fond of creating get functions to return data from a class, but I have discovered that Zend are fond of declaring member variables public, and also the creators of DOMDocument prefer that, too.

In DOMDocument's case, they seem to prefer you access public member variables when there isn't any requirement to specify an argument. For example, in nodeValue and childNodes, neither require an argument to be passed because both return explicit data.

In a simple example:

php Code:
$pA = $pDom->query('div a');
$pDiv = $pA->parentNode;

The class knows precisely what it's getting the parent node of because you're calling the function on the particular node you're working with.

You will also see the same type of thing in Zend Framework whereby public member variables are accessed when retrieving data from a table.

For instance:

php Code:
$pMember = $this->getTable('Members')->fetchRowByUsername('Wildhoney');
echo $pMember->username;

However, particularly in DOMDocument's case, anything that requires an argument naturally becomes a function. In the case of getAttribute. You must specify which attribute you're wanting to retrieve and so having this as a member variable, whilst possible in the sense that you could have every attribute as a member variable, is perhaps somewhat impractical.

To give another example of this:

php Code:
$pNode->getAttribute('onclick');

This would of course retrieve the onclick attribute of the referenced node.

So now you can see why I am curious. I myself have always preferred using get functions, as aforementioned. Again, it may seem somewhat overkill to create a get function for each and every set function, and also each and every item of data your class holds, but it's certainly possible to create a the magic method __call to give the illusion of individual functions.

Yet another example:

php Code:
public function __call($szFunction, $aArguments)
{
    printf("You called the function: %s", $szFunction);
}

I suppose it's all down to personal preference, but, as many do when posting to TalkPHP, I myself use the Hungarian notation when coding, and those look especially peculiar when calling a pub member variable. I must admit that whilst I do use Hungarian notation when creating private member variables, I would be hesitant to do when creating their public equivalents.

I would prefer:

php Code:
$pClass->myClassData;

To this:

php Code:
$pClass->szMyClassData;

So, my question is, my little personal experiment:

Who uses public member variables to retrieve the data, and who creates a multitude of get functions? Would you consider using the __call method just to have the ability to use functions as getUsername when not actually creating a getUsername function?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
feedback on my class please frostyboy33 Advanced PHP Programming 7 10-22-2012 09:12 AM
base classes..... allworknoplay Absolute Beginners 16 05-10-2009 08:09 PM
Extends? Implements? Private, Public, Protected? Aaron Advanced PHP Programming 13 05-07-2008 02:38 AM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 04:23 PM


All times are GMT. The time now is 02:12 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design