View Single Post
Old 06-08-2009, 01:42 AM   #1 (permalink)
Wildhoney
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