__get() and __set()

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

arborint wrote:
timvw wrote:Do we actually want properties? Here are a couple of observations from the C# world:
I find this question a little startling because properties are really an essential part of objects.
But how would you define properties? I don't see how they are any different than other methods...

There are (imho) two options:

- Or you consider the property as a regular method (controlled access to the object's data)
- Or you consider the property as (uncontrolled) direct access to a piece of data in the object... This results in the same effect as making the data public...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

As far as finding out if the property is available by trying '$obj->prop = 1' etc.. how does that differ from any other accessor?

object chaining has been around since objects have been in the php engine :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Jenk wrote:As far as finding out if the property is available by trying '$obj->prop = 1' etc.. how does that differ from any other accessor?

object chaining has been around since objects have been in the php engine :)
$obj->prop = 1

I think this is sort of the point. Do you want developers to make assignments to properties in this fashion? There problem, not yours no? Ohh... email support can get very tedious when it's down to people doing things they shouldn't be doing ;)

Not only that. Let's take PHPMailer as an example of where accessors should have been used.

$mail->Body = 'Foo';

Ok, good that works.

How about:

$mail->body = 'Foo';

No? WTF? Mail not sending. But I specified a body and it never gave me an error :? Hmm... Let's email support :)

Properties are an integral part of objects, I don't think that's in dispute ;) Accessing them should be controlled (ok ok so there are varying views on this) IMO.

__get() and __set() are still accessors. They just allow access control to be handled via properties.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

My point was referring to how does 'testing' if an accessor works by simply trrying it differes from using a direct property assignment, to a setter?

They are in essence the same is the answer :)

$obj->prop = 1;

May work, may fail.

$obj->setProp(1);

May work, may fail. We don't know yet :)

It just seemed that a point was being made that setters are more resillient to the above than directly setting properties.. which is wrong, they are 'equal' on this matter. :) (Besides, if it's properly done it'll have all the doc's needed :p)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I like to use __isset() too. :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

.. and __call() is normally used by me :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Jenk wrote:My point was referring to how does 'testing' if an accessor works by simply trrying it differes from using a direct property assignment, to a setter?

They are in essence the same is the answer :)

$obj->prop = 1;

May work, may fail.

$obj->setProp(1);

May work, may fail. We don't know yet :)

It just seemed that a point was being made that setters are more resillient to the above than directly setting properties.. which is wrong, they are 'equal' on this matter. :) (Besides, if it's properly done it'll have all the doc's needed :p)
Actually, when you're programming against the interface you know exactly which methods exists...
On the other hand, you don't know which variables/properties the implementation will have...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Ah yes.. interfaces.
Post Reply