![]() |
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:
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:
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:
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:
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:
To this: php Code:
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? |
Is there any chance this poll could allow multiple options to be ticked?
|
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 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:
Individual Methods I use ind. functions for data I will be calling that is a class property. PHP Code:
__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 |
Quote:
|
| All times are GMT. The time now is 05:55 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0