Re: PHP classes



Curtis wrote:
On Sat, 27 Dec 2008 19:25:44 -0500, jstucklex@xxxxxxxxxxxxx wrote:
Curtis wrote:
On Fri, 26 Dec 2008 17:43:02 -0500, jstucklex@xxxxxxxxxxxxx wrote:
Curtis wrote:
On Tue, 23 Dec 2008 02:29:32 -0800 (PST), marksmith5555@jungle-
monkey.com wrote:
I am starting to write some of my first classes with php. And I've
noticed something about php that can make this very error prone.

In all other object oriented languages I have used when inside a
class, I can access member variables by name alone. However in php,
doing so just declares a local variable. Ommitting the $this->variable
part has been the cause of over half my bugs in the last week.

It is such an easy mistake to make, I was wondering if there was any
way to work around this and force php to use a member variable (if it
exists) rather than redeclare locally?

Thanks for any suggestions.
If I plan to use a member variable locally in a methid, and need to reflect the changes in the actual member variable, I will sometimes use references.

For example, something like:

public function foo( )
{
$si = &$this->stack_index;
...
}

Which is a total violation of OO principles, as Sheldon indicated.
I suppose that should have come with a disclaimer, as I'm no expert with the OO paradigm.

I'll have to look into why that's bad. Thanks.

Look into encapsulation and why it exists.

Thanks for the pointer.

Once you understand encapsulation, it should become obvious why
returning a reference to a member is bad.

Just to clarify, in my example, I didn't specify returning a reference to a member (the function isn't defined to return a reference, either). I think I made the code brief, to a fault. What I meant was to use the reference only within the function as a shorthand for $this->stack_index.

Perhaps this is still a poor implementation decision, I'm not sure.


OK, I thought by the shortness of it you were returning a reference.

The way you're using it is OK. I think it can be a bit confusing and error prone, especially if you get a long function. And what happens if you change $si to point to something else (i.e. via a mod)? Suddenly code which expects it to point to $this->stack_index will find it pointing to something else. But that's more personal preference than anything.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: PHP classes
    ... noticed something about php that can make this very error prone. ... In all other object oriented languages I have used when inside a ... I can access member variables by name alone. ... returning a reference to a member is bad. ...
    (comp.lang.php)
  • Re: PHP classes
    ... noticed something about php that can make this very error prone. ... I can access member variables by name alone. ... returning a reference to a member is bad. ...
    (comp.lang.php)
  • Re: PHP classes
    ... noticed something about php that can make this very error prone. ... I suppose that if I learned PHP first and then switched to C++ I might wonder while I don't have to refer to member variables as this->member...and think that could be terribly error prone...as I'd never know without looking at the class if I was referring to a global or file scoped static, ... If you did not mean C++ by all other object oriented languages, replace C++ with the appropriate language list to come up with your own mantra. ...
    (comp.lang.php)
  • Re: PHP classes
    ... noticed something about php that can make this very error prone. ... In all other object oriented languages I have used when inside a ... I can access member variables by name alone. ...
    (comp.lang.php)
  • Re: PHP classes
    ... noticed something about php that can make this very error prone. ... In all other object oriented languages I have used when inside a ... I can access member variables by name alone. ...
    (comp.lang.php)