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

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
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
Old 06-08-2009, 09:09 AM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Is there any chance this poll could allow multiple options to be ticked?
Salathe is offline  
Reply With Quote
Old 06-08-2009, 10:26 AM   #3 (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
Default

Yes, I tried to modify it to be so, but it would appear you can't make it multiple choice once submitted. Huge flaw!
__________________
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
Old 06-08-2009, 07:06 PM   #4 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

I use the top 2 .... depending on where and how I will be retrieving the data and what the data is that I will be retrieving.
Example for each
------------------------------------------------------------------
Public Member Variables
Almost 99% of all public calls I make are routed into overloading using __get, __set, __isset, __unset which are taken from a registry

PHP Code:
$a = new Foo_Top();
echo 
$a->username
Will overload into something such as __get() -> Static_Registry()::get() -> return Or null

Individual Methods
I use ind. functions for data I will be calling that is a class property.
PHP Code:
$b = new Foo_Jar();
$b->getAppleCount(); 
returns the number of apples, same goes for setting, while it might seem pointless, it is much to debug and write code knowing exactly where and when you get and set a property

__call Overload
Most code I will write does not allow __call overloading

Function When Arguments Required (Methods)
If you are requiring arguments, you will more than likely be performing some kind of logic; you are not really explicitly retrieving data only within the class as it will is likely to be manipulated in some form
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 06-08-2009, 10:16 PM   #5 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Yes, I tried to modify it to be so, but it would appear you can't make it multiple choice once submitted. Huge flaw!
I aswell want to tick multiples. I'm very fond of different methods together.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Reply



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 04:12 AM.

 
     

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