[TYPO3-ect] News about XCLASSES?
Ernesto Baschny [cron IT]
ernst at cron-it.de
Wed Aug 23 09:44:40 CEST 2006
Bernhard Kraft schrieb am 22.08.2006 22:58:
> I have an XCLASS manager which allows you to change the order which file XCLASSes which other
> so you can "chain" XCLASSes so they work together - this is done using an BE module which displays
> the XCLASS structure graphicall in a tree.
Sound interesting, but will then more than one class XCLASSing the same
class work?
> But I already tought days and nights about how to solve the XCLASS problem ... I didn't find any proper
> solution ...
>
> I do not know very much about patterns (except the singleton pattern) but I did never really get it how
> an decorator pattern could achive it that I dynamically change methods of specific classes .... at runtime.
>
> If you have a working method please let me know and show me some code :)
As far as I know about the decorator pattern, it works like that:
You create a new class that implements the complete interface of the
original class (i.e. it provides all methods from the "inherited"
class). The constructor gets an instance of the original class and you
start with calling all original methods on that instance that you get
passed. Then you can simply add or change or enhance certain methods to
do what you want it to do. As the constructor gets an instance of the
class with the same interface, it is easy to "chain" together many classes.
This is not very difficult. But the challenge is then to find a way for
TYPO3 to make the correct "chain" of classes once it is called.
So imagine one wants to enhance cObject's IMGTEXT method. I create a class:
class cObjectIMGTEXTEnhanced {
var $cObj;
function cObjectIMGTEXTEnhanced(&$cObj) {
$this->cObj =& $cObj;
}
function start($data,$table='') {
return $this->cObj->start($data, $table);
}
function setParent($data,$currentRecord) {
return $this->cObj->setParent($data,$currentRecord);
}
....
function IMGTEXT($conf) {
// do our "modified" IMGTEXT rendering here...
$content = "Yeah...";
// this might even call $this->cObj->IMGTEXT($conf) and process its
// output
return $content;
}
....
}
Note that I *must* implement the complete interface of cObject, because
I am not subclassing it! In Java/PHP5 there should be an interface for
every class that I can then implement so that the contract is guaranteed
to work.
When TYPO3 then needs to create a cObj and our extension has
"registered" somehow that we provide an enhancement to it, the
makeInstance method just has to chain that together:
function &makeInstance($className) {
$obj =& new $className;
$decorators =
$GLOBALS['TYPO3_CONF_VARS']['SYS']['OBJ_DECORATORS'][$className];
foreach ($decorators as $dec) {
$obj =& new $dec($obj);
}
return $obj;
}
This is untested, I just made it out! :)
So here you have it, enhancing any class in TYPO3 even several times,
without using XCLASS!
Cheers,
Ernesto
More information about the TYPO3-team-extension-coordination
mailing list