[TYPO3-dev] SOLUTION (?) : Multiple XCLASS inheritance

Bernhard Kraft kraftb at kraftb.at
Tue Feb 7 23:50:09 CET 2006


Jerome Schneider wrote:

Hi !!!

I think I tortured my head for days thinking about how there would be a way to
"merge" methods of overruling "Y"CLASSES :) with a base class. This can be done
in OO programming languages using a decorator pattern AFAIK.

But I couldn't think of any way how to do this in PHP.

I tried a little bit around if it would be possible to have variable names as
class names like

$class = 't3lib_TCEmain';

class $class {

/* methods go here */

}

but PHP didn't like that :(

So let me ask if I understood you correctly:

>  class tx_forum_old extends pi_base
>  {
>   function main()
>   {}
> 
>   /* some code */
>   /* this is the original method */
>  }
> 
>  class tx_forum_extended extends tx_forum_old
>  {
>   /* our custom methods, overloading tx_forum */
>  }
> 
>  class tx_forum extends tx_forum_extended  /* this is an empty, dummy class 
> :) */
>  {}       /* that is the trick */
>         /* this presents to typo3 the modified tx_forum class with it's 
> classic name */

An "meaningfull" implementation would be:

The "original" file t3lib_add.php :) :
-----------snip-----
<?php
class tx_add_old extends tx_baseofall	{

	function add($a, $b)	{
		/* Original base functionality */
		return intval($a)+intval($b);
	}

}

if (!$TYPO3_CONF_VARS['YCLASS']['t3lib/t3lib_add.php'])	{
	class tx_add_extended extends tx_add_old	{
		/* Kind of "connector" class when no YCLASS hooks in ??? correct */
	}
} else	{
	require_once(t3lib_div::getFileAbsFileName($TYPO3_CONF_VARS['YCLASS']['t3lib/t3lib_add.php']));
}


class tx_add extends tx_add_extended	{
	/* empty dummy class - has to get instanciated */
}

?>
-----------snip-----

and then in a ext_localconf.php the line:

$TYPO3_CONF_VARS['YCLASS']['t3lib/t3lib_add.php'] = 'EXT:ext_something/class.ux_add_2d.php';

and the file typo3conf/ext_something/class.ux_add_2d.php:
------------------snip------------
<?php

class tx_add_extended extends tx_add_old	{
		/* Extra functionality YCLASS */
	function add($a, $b)	{
			// Perform 2dimensional addition
		list($ax, $ay) = explode(',', $a);
		list($bx, $by) = explode(',', $b);
		return (intval($ax)+intval($bx)).','.(intval($ay)+intval($by));
	}
}


------------------snip------------


If you can just answer or give some comments on my /* */ remarks I think I will know what I would like to.

The only thing I don't really get is "how" more than one class should be able to hook in when one
doesn't know of each other ... which normally is the problem with XCLASSES ...

In my opinion this "reverse" inheritance only moves the point where the problem occurs :(

If you have 2 XCLASS extensions which XCLASS the same file but different methods it is already easy right
now to also have a ux_ux_baseclass class in one of the extensions which get's included if the other extension
is installed...

but it would be great if one extension wouldn't have to know about the other.

The problem of overlapping methods could get solved by checking for same method names in the different class
files
(probably using the php tokenizer :)
an warning should get shown in the EM if the installation should get aborted or tried anyway.

greets,
Bernhard




More information about the TYPO3-dev mailing list