[TYPO3-mvc] Casting objects

Xavier Perseguers typo3 at perseguers.ch
Wed Jul 8 08:16:40 CEST 2009


Hi,

Is there some official and more important working way of casting objects 
when using Extbase?

I have a class B (in extension FooBar) extending class A (from extension 
Foo).

I only want to extend it for my own purpose in EXT:FooBar, thus no use 
of XCLASS.

Now, I have to override a few methods such as FindOneBy... in order to 
return a class B object instead of class A. What should I do?

I tried to do this:

public static function castToClass($class, $object) {
	return unserialize(preg_replace('/^O:\d+:"[^"]++"/', 'O:' . 
strlen($class) . ':"' . $class . '"', serialize($object)));
}

and

public function findOneByUid($uid) {
	return FooBar_Helper::castToClass('B', parent::findOneByUid($uid));
}

But it does not work:

Warning: Invalid argument supplied for foreach() in 
/path/to/extbase/Classes/DomainObject/AbstractDomainObject.php on line 58

It fails after the call to unserialize in my castToClass function, when 
magic method __wakeup on AbstractDomainObject is called because

$GLOBALS['Extbase']['reconstituteObject']['properties']

is not defined in my case.

Any better idea?

-- 
Xavier Perseguers
http://xavier.perseguers.ch/en

One contribution a day keeps the fork away


More information about the TYPO3-project-typo3v4mvc mailing list