Re: How can I do the opposite of property_exists(), maybe a create_property() in PHP5?
| From: | Nathan Nobbe | Date: | Tue, 03 Feb 2009 23:42:13 +0000 |
| Subject: | Re: How can I do the opposite of property_exists(), maybe a create_property() in PHP5? | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-287591@lists.php.net to get a copy of this message | ||
On Tue, Feb 3, 2009 at 4:19 PM, Daevid Vincent <daevid@daevid.com> wrote:
> On Wed, 2009-02-04 at 08:58 +1100, Chris wrote:
>
> > >> the question is what is __set() doing, if it's throwing an exception
> > >> for undefined properties then obviously it with 'blow up'.
> > >
> > >
> > >
> > > But why should __set() even be called if I'm accessing the property
> > > directly? This seems stupid.
> > >
> > > $this->oraclecustomerid = 1122;
> > >
> > > should NOT be the same as
> > >
> > > $this->set_oraclecustomerid(1122);
> > >
> > > The second one I agree should call __set(), but the first one should
> NOT
> > > be triggering __call() or __set()
> >
> > Yes it should.
> >
> >
>
> http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members
> >
> > __set() is run when writing data to inaccessible members.
> >
> > if it's a protected/private variable, it'll call __set.
> >
> > If it's a variable that doesn't exist, it'll call __set.
> >
>
>
> Let me rephrase that. I see now that it is designed that way, but I
> think the logic is erroneous. While I'm sure this argument/discussion
> is all for naught now, I believe that a straight assignment of a value
> to a variable, SHOULD NOT do any behind the scenes magic __set(). It
> should just do it. Otherwise, what's the point of being able to "set" a
> property/variable both ways? One gives no benefit over the other and as
> illustrated decreases flexibility. It appears it will work if I change
> my property to public, but I don't want them exposed like that. *sigh*
>
> Bottom line is there should be a create_property($name, $value = null,
> $type = 'protected') function/method that I can call to do what I'm
> trying to do.
>
> I assume unset($this->foo); works. So therefore, I can check for
> existence of a property, and consequently remove a property, but I
> cannot create a property.
wow, obviously you can create properties at runtime. if you want direct
access to property assignment, dont define __set() for that class. if you
want to override this assignment, then define __set() for that class, pretty
simple..
and property creation / assignment is essentially the same thing, since all
properties must store a value. when you 'create' a property in php w/o
explicitly giving it a value the default value is NULL.
i recommend that if you want to keep __set() defined in this class you
mentioned, and not have the melt-down b/c you have some check to see if the
property exists, you can just define another method, createOrSet($property,
$value), something to that effect, which will ignore the step about
verifying the property already exists.
-nathan