Greg Beaver wrote: > Wouter, > > you could try this adding this method to each object you need > blessings from: > > function &bless($classname) > { > if ($classname == get_class($this)) { > return $this; > } > $vars = get_object_vars($this); > $ret = new $classname; > return $ret->loadValues($vars); > } > > function &loadValues($vals) > { > foreach ($vals as $name => $val) { > $this->$name = $val; > } > return $this; > } > > In the Load() method, you should determine what class you > need, and call $ret = &$this->bless('classname'), and then return > $ret. > > Then, instead of doing > > $Thing->Load(); > > do > > $Thing = &$Thing->Load(); > > and have Load() return an object instance (either $this or the newly > blessed object). > > This will maintain encapsulation and achieve the results you're > looking for. > > Regards, > Greg >
Thanks Greg .. this comes pretty close to what I had done myself as a workaround. Only thing that's different is that in my bless implementation I don't return the blessed value, but overwrite the $this var. Which works. What advantage do you think I would get from your appraoch? [snip The Way I Bless {example from own memory, cannot reach the actual code at this time} ] function Bless($ClassName) { // return false if class doesn't exist if (!class_exists($ClassName)) return false; $New = new $ClassName(); foreach($this as $Key => $Value) $New[$Key] = $Value; $this = $New; unset $New); } [/snip The Way I Bless] Hmm .. maybe I'm thinking 'out of te box' here, but can I manually add this functionality to stdClass, so that they are available in each and ever object I create? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php