override __set in __construct() in php?
Posted
by user151841
on Stack Overflow
See other posts from Stack Overflow
or by user151841
Published on 2010-05-04T14:46:40Z
Indexed on
2010/05/04
14:48 UTC
Read the original article
Hit count: 177
I have a class based on database values. I'm using __set
to automatically sync database values with the class properties. Set checks an array of database fields that it is allowed to update in the database. The field 'id' isn't in the list, so __set
will throw an exception if you try to do $objDbRow->id = 5;
.
However, there is one time when I do want to set the id
property of the object, and that's on instantiation. So in __constuct
, I have $this->id = $id
(where $id
is passed to __construct
). However, __set
seems to be intercepting the setting here, because an exception is being thrown on construction.
What's the way to get around this? I suppose I also have a boolean flag, like $instantiated
, that __set()
would check before it does it's field whitelist checking. But that feels inelegant.
© Stack Overflow or respective owner