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]

 



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;
>>                                }
> 
> 
> Hey,
> well, $this->$pkey is wrong syntax. Try $this->pkey = $value
> 

there is nothing wrong with $this->$pkey.

the question is what is __set() doing, if it's throwing an exception
for undefined properties then obviously it with 'blow up'.

I would suggest looking into using an array inside the object to
store all user data, you can still use some setter methods for user fields
that are known at compile time and for the rest you just stuff the extra/unknown
fields into the array, something like:

class Test {
	private $data = array();

	function loadUserData($data) {
		foreach ($data as $key => $val) {
			if (method_exists($this, 'set_'.$key)
				$this->{'set_'.$key}($val);
			else
				$this->data[ $key ] = $val;
		}
	}
}



> -eddy
> 
> 
> 
>>                                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();
>> }
>> ?>
>>
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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