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 08:30 +0100, Edmund Hertle wrote:

> 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;
> >                                }



> Hey,
> well, $this->$pkey is wrong syntax. Try $this->pkey = $value


No Eddie, it's one of the beautiful, simple and powerful things about
PHP. 
http://www.php.net/manual/en/language.variables.variable.php

As I loop over the hash, i am TRYING to create a new class property of
the key and assigning it the value.
$pkey is basically the hash's $key in mixed case, forced to lowercase.

You can do this for variables and for functions/methods too. This is a
'factory'. I've used it for example for parsing an XML file and
operating on the data within various 'blocks' by reading the <block
name="foo" value="bar"> and then executing $$name($value). 

Thanks for trying though. ;-)


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