Re: PHP classes
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Sat, 27 Dec 2008 20:16:50 -0500
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:Look into encapsulation and why it exists.Curtis wrote:I suppose that should have come with a disclaimer, as I'm no expert with the OO paradigm.On Tue, 23 Dec 2008 02:29:32 -0800 (PST), marksmith5555@jungle-Which is a total violation of OO principles, as Sheldon indicated.
monkey.com wrote:
I am starting to write some of my first classes with php. And I'veIf 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.
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.
For example, something like:
public function foo( )
{
$si = &$this->stack_index;
...
}
I'll have to look into why that's bad. Thanks.
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
==================
.
- Follow-Ups:
- Re: PHP classes
- From: Curtis
- Re: PHP classes
- References:
- PHP classes
- From: Mark Smith
- Re: PHP classes
- From: Curtis
- Re: PHP classes
- From: Jerry Stuckle
- Re: PHP classes
- From: Curtis
- Re: PHP classes
- From: Jerry Stuckle
- Re: PHP classes
- From: Curtis
- PHP classes
- Prev by Date: Re: PHP classes
- Next by Date: MySQLi or PDO or PEAR DB?
- Previous by thread: Re: PHP classes
- Next by thread: Re: PHP classes
- Index(es):
Relevant Pages
|