Re: How can I do the opposite of property_exists(), maybe a creat_property() in PHP5?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2009-02-03 at 12:51 +0100, Jochem Maas wrote:

> Edmund Hertle schreef:
> > 2009/2/3 Daevid Vincent <daevid@xxxxxxxxxx>
> > 
> >> Is there a way to create a new property via PHP 5.2.4?
> >>
> >> I get a hash back from an authentication server. I'm not guaranteed that
> >> someone in another department won't add new key/values to the returned
> >> hash/array. I'm trying to work around that part gracefully so that the
> >> code doesn't blow up on a customer in such an event. The main try/catch
> >> will suppress errors already, but I thought it would be nice to be able
> >> to handle this stuff automatically rather than constantly updating a
> >> User.class.php file all the time.
> >>
> >>    "creating new property this->oraclecustomerid with 1122"
> >>
> >> but when I try to set the value with the $this->$pkey = $value;
> >>
> >> It triggers __call() which then triggers __set() which throws my
> >> BadProperty exception.
> >>
> >> How come $this->$pkey = $value isn't creating/setting a property?
> >> Or how do I do something like create_property($this, $pkey);
> >> so that I can then set it via $this->oraclecustomerid = 1122 or
> >> $this->set_oraclecustomerid(1122) ???
> >>
> >> <?php
> >> function load_from_user_data($user_data)
> >> {
> >>        //now loop through the rest of the user_data array and assign via a
> >> set_foo() method
> >>        foreach ($user_data as $key => $value)
> >>        {
> >>                        //try
> >>                        {
> >>                                $pkey = strtolower($key);
> >>                                //[dv] this is sort of a hack to
> >> "automatically" create a new
> >> property/variable
> >>                                //         for 'new' hashes key/values we
> >> may not know about.
> >>                                //         It's really designed to supress
> >> errors and they really should
> >> be added to this User.class.php properly.
> >>                                if ( !property_exists($this, $pkey) )
> >>                                {
> >>                                        echo "creating new property
> >> this->$pkey with $value<br>\n";
> >>                                        $this->$pkey = $value; //THIS BLOWS
> >> UP ON THE __set()
> >>                                        echo "this->$pkey = ".$this->$pkey;
> >>                                }



> 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()


> > 
> > 
> > 
> >>                                else
> >>                                {
> >>                                        $class_variable = 'set_'.$pkey;
> >>                                        $this->$class_variable($value);
> >>                                        unset($user_data[$key]);
> >>                                }
> >>                        }
> >>                        //catch (Exception $e)
> >>                        {
> >>                                //echo $e->getMessage()."\n";
> >>                        }
> >>        }
> >>
> >>                //should new fields be returned in the $user_data that are
> >> not
> >> accounted for above...
> >>                if ($_SESSION['DEVELOPMENT'] && count($user_data))
> >>                {
> >>                        echo "<!-- Unaccounted for user_data hashes. Please
> >> add these into
> >> User.class.php:\n";
> >>                        var_dump($user_data);
> >>                        echo "-->";
> >>                }
> >>
> >>        //THESE TWO LINES FATAL ERROR ON THE __get():
> >>        echo "this->oraclecustomerid = ".$this->oraclecustomerid;
> >>        echo "this->get_oraclecustomerid() =
> >> ".$this->get_oraclecustomerid();
> >> }
> >> ?>
> >>
> > 
> 



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux