[TYPO3-dev] Extending classes on demand

Franz Holzinger franz at fholzinger.com
Tue Aug 29 07:40:31 CEST 2006


Hello Johannes,

> So the "normal" class that is used in typo3 is instantiated already by 
> typo3 while execution? that is why $this works? is $this right? :)
$obj = &t3lib_div::getUserObj('tx_xy_extended');
$obj = t3lib_div::makeInstance('tx_xy');

This are the functions where TYPO3 instantiates objects.

ExtDevEval:
> function &getUserObj($classRef,$checkPrefix='user_',$silent=0)
> 
> Creates and returns reference to a user defined object.
> This function can return an object reference if you like. Just prefix the function call with "&": "$objRef = &t3lib_div::getUserObj('EXT:myext/class.tx_myext_myclass.php:&tx_myext_myclass');". This will work ONLY if you prefix the class name with "&" as well. See description of function arguments.
> Usage: 5
> $classRef 	string 	Class reference, '[file-reference":"]["&"]class-name'. You can prefix the class name with "[file-reference]:" and t3lib_div::getFileAbsFileName() will then be used to resolve the filename and subsequently include it by "require_once()" which means you don't have to worry about including the class file either! Example: "EXT:realurl/class.tx_realurl.php:&tx_realurl". Finally; for the class name you can prefix it with "&" and you will reuse the previous instance of the object identified by the full reference string (meaning; if you ask for the same $classRef later in another place in the code you will get a reference to the first created one!).





> 
> the problem i have is that the scope is always changing and i do not see 
> a convinient way to "inline-extend" a class without problems. it would 
> be just very comfortable to have constructs like this:
> 
> class foo {
> require_once('class.extends_foo.php');
> }
> 
> // other php file
> extends( {
> .../blaaa
> }
> 
> and in both of them you can access everything like it was one - only 
> that you load the extends stuff on demand. but i guess i am thinking in 
> the wrong direction, on the other hand i do not understand why this 
> won´t work. i would get rid of scope and variable problems quickly... 
> hrmpf ;)
> 
The PHP5 interfaces have been implemented to do such a thing.

class tx_xy_extended implements tx_xy {
    function foo() {
       return '***';
    }
}


interface tx_xy {
    function foo();
}


Then you can create functions where you can pass any object as parameter 
which implements the interface tx_xy.
The interface itself does not have any code but only a listing of the 
function names with parameters.

function processFoo($obj) {
    $obj->foo();
}

$obj1 = &t3lib_div::getUserObj('tx_xy_extended1');
$obj2 = &t3lib_div::getUserObj('tx_xy_extended2');
...
$objn = &t3lib_div::getUserObj('tx_xy_extendedn');


$this->processFoo($obj1);
$this->processFoo($obj2);
...
$this->processFoo($objn);


- Franz










More information about the TYPO3-dev mailing list