[Typo3-dev] [Good||Silly] idea ???
dan frost
dan at danfrost.co.uk
Tue Mar 8 13:41:14 CET 2005
The main problem with XCLASSes in TYPO3 is the lack of a separation of
concerns - people are extending one class for several reasons. There
should be several classes.
Nice idea though - have you come across Aspect Oriented Programming?
Might make things easier...
dan
Kraft Bernhard wrote:
> Hallo list,
>
>
> Since I know about XCLASSes and how they work I was looking for a method
> to extend classes
> in a more clever way than until now.
>
> Because at the moment a extensio can extend a core class, but if the
> core-class is already extended
> by another extension those two extensions won't work together and the
> last one installed will
> take precedence.
>
> But if those two extensions modify completly different functions from
> the base class they could
> exist together...
>
> The only workaround at the moment is that one extension knows about the
> other one and extends the
> extension-class instead of the core class.
>
> -----
>
> The main problem when doing XCLASS extensions is that the classname of
> the extending class must be
> set to a static value. If the classname could be a dynamic value the
> complete problem would be solved.
> I tried:
> $classname = 'baseclass';
> and
> define('CLASSNAME', 'baseclass');
> but none of them did work ...
>
> After investigating a little further and asking on some mailing lists I
> came to the following POSSIBLE
> solution:
> write the class definition in an extra file without leading "<?php" and
> trailing "?>" tags.
> then load this file via "implode('', file($filename))".
> then replace the markers for baseclass and extended class.
> then evalute the script.
>
> Here are some sample scripts which show what I tried (and worked !!):
> -------------------------<index.php>------------------------------------
> <?php
> define('TYPO3_MODE', 'FE');
> // Those lines should be in ext_localconf.php of the extensions
> $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS2']['t3lib/class.t3lib_testphp.php'][]
> = 'extend1.php';
> $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS2']['t3lib/class.t3lib_testphp.php'][]
> = 'extend2.php';
>
> class baseclass {
> function a() {
> echo 'I\'m function "a" from base class !<br>'.chr(10);
> }
> function b() {
> echo 'I\'m function "b" from base class !<br>'.chr(10);
> }
> function c() {
> echo 'I\'m function "c" from base class !<br>'.chr(10);
> }
> }
> function &makeInstance($className) {
> return class_exists('ux_'.$className) ?
> makeInstance('ux_'.$className) : new $className;
> }
> // XCLASS2 inclusion
> if (defined('TYPO3_MODE') &&
> is_array($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS2']['t3lib/class.t3lib_testphp.php']))
> {
> foreach
> ($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS2']['t3lib/class.t3lib_testphp.php']
> as $inc) {
> include_once($inc);
> }
> }
> // Main
> $main = makeInstance('baseclass');
> $main->a();
> $main->b();
> $main->c();
> ?>
> -------------------------</index.php>------------------------------------
>
> -------------------------<extend1.php>------------------------------------
> <?php
> $basename = 'baseclass';
> $extname = 'ux_baseclass';
> while (class_exists($extname)) {
> $basename = 'ux_'.$basename;
> $extname = 'ux_'.$extname;
> }
> $data = implode('', file('extend1_code.php'));
> $data = str_replace('###EXT_CLASS###', $extname, $data);
> $data = str_replace('###BASE_CLASS###', $basename, $data);
> eval($data);
> ?>
> -------------------------</extend1.php>------------------------------------
>
>
> -------------------------<extend1_code.php>------------------------------------
>
> class ###EXT_CLASS### extends ###BASE_CLASS### {
> function a() {
> echo 'I\'m function "a" from extend1 class !<br>'.chr(10);
> }
> }
> -------------------------</extend1_code.php>------------------------------------
>
>
>
> -------------------------<extend1.php>------------------------------------
> <?php
> $basename = 'baseclass';
> $extname = 'ux_baseclass';
> while (class_exists($extname)) {
> $basename = 'ux_'.$basename;
> $extname = 'ux_'.$extname;
> }
> $data = implode('', file('extend2_code.php'));
> $data = str_replace('###EXT_CLASS###', $extname, $data);
> $data = str_replace('###BASE_CLASS###', $basename, $data);
> eval($data);
> ?>
> -------------------------</extend1.php>------------------------------------
>
>
> -------------------------<extend1_code.php>------------------------------------
>
> class ###EXT_CLASS### extends ###BASE_CLASS### {
> function b() {
> echo 'I\'m function "b" from extend2 class !<br>'.chr(10);
> }
> }
> -------------------------</extend1_code.php>------------------------------------
>
>
>
> After calling index.php the follwing gets outputed:
> -------------------------<output>----------------------------------------
> I'm function "a" from extend1 class !
> I'm function "b" from extend2 class !
> I'm function "c" from base class !
> -------------------------</output>----------------------------------------
>
>
> So is this a good idea and should we think about a new (possible
> different) way of doing XCLASS
> extending ???
>
> Or is this "eval" thingy just damn slow and one shouldn't use it ?
>
> Or are there any other reasons why such a style isn't good practice ?
>
> This way of XCLASSing would also eliminate the problem to have the same
> code in two locations when
> you extend two different baseclasses (i.E. class.t3lib_stdgraphic,
> class.tslib_gifbuilder) with the
> same code ... (as you would just have to include the same file in two
> locations and change the classnames
> accordingly)
>
>
> greets,
> Bernhard
More information about the TYPO3-dev
mailing list