|
John-Marc Ventimiglia - 2012-03-21 15:46:42
var $variable name;
.. and even if I put in an underscore, I am stupid. What is it supposed to show?
$variable_name='FOO';
class class_name
{
var $variable_name;
function function_name()
{
echo $this->variable_name;
}
};
$object = new class_name;
$object->function_name();
This displays nothing. Shouldn't it display Foo?
Manuel Lemos - 2012-03-21 16:00:25 - In reply to message 1 from John-Marc Ventimiglia
You are right, there was an underscore missing.
I also added some value to initialize the variable, so it prints something.
Anyway, this was just a simple demonstration of how global code should look like after converted to a class for those that do not know OOP well enough yet.
John-Marc Ventimiglia - 2012-03-21 20:31:03 - In reply to message 2 from Manuel Lemos
OK, I see what I did wrong
>Anyway, this was just a simple demonstration ...
A bit too simple IMO. Please consider your target audience. People like me with NO CLUE on OOP.
The example var $variable_name = 'foo'; is pretty uselss as well. The question begs ... how do I get data into the class?
For someone who the light just went on for, I can see that what I REALLY needed to see was that this in procedural:
$variable_name = 'foo';
becomes this in OOP
$object->variable_name = 'foo';
Yes, it should be obvious, but us beginners are tripping over our own shoe strings.
Thank you for this. There is one other very useful reason for OOP, and I am now rewriting many functions thanks to you.
In procedural, I write a function that accepts 2 parameters. Eventually I find I need more and more parameters, and soon I have no clue what these parameters do looking at code, unless I go back to the function.
$data=confused('A','Z','T');
However within the code there is more clarity with OOP - it is basically self-documenting
$object = new confusion;
$object->first_letter='A';
$object->last_letter='Z';
$object->any_old_letter='T';
$object->confused();
Manuel Lemos - 2012-03-21 22:19:21 - In reply to message 3 from John-Marc Ventimiglia
Right, the focus of the article was to migrate global code to classes. What you do with the classes after you migrate it is up to you.
The article tells you about the $this operator to manipulate variable values from inside the classes.
From outside the classes you can use the object or eventually some setter and getter functions, but I did not want to go there because it would require a greater explanation about why getter and setter functions would be better.
Anyway, since you ask about passing many parameters to the class, using class variables may or may not be the right way to do it. Usually you use class variables to configure parameters that may be used by multiple functions, although there is nothing wrong to use them for configuring behavior that only concerns one function.
What I mean is that you need to evaluate case by case on whether it makes sense to use class variables just to avoid passing more parameters to functions or not.
|