[Typo3-dev] Alternative to hooks - fake "$this"

dan frost dan at danfrost.co.uk
Thu Oct 28 15:37:09 CEST 2004


Two ways:
- is there a parent class somewhere? or a similar class without the 
output generated at the bottom?

- make a new, identical class in another file without the output

I nearly did this myself with the db_list

dan

Frank Benkart [DIACC] wrote:
> Hi Dan,
> nice idea! I would like to test it.
> 
> But how can I use the class typo3/move_el.php in my own extension?
> When including this file, automatically an output is generated, because the
> file includes a class and the intantiating of this class. That's not e rally
> nice architecture....
> 
> What do you think about this?
> 
> ---------------------------------------------
> live strong - live typo3
> 
> 
> "dan frost" <dan at danfrost.co.uk> schrieb im Newsbeitrag
> news:mailman.1.1098957618.12231.typo3-dev at lists.netfielders.de...
> 
>>Dear all,
>>
>>Here's a trick i've used recently to fake the idea of hooks:
>>
>>First, we have a class which makes a query at some point:
>>
>>class first_class {
>>   function main() {
>>     //...do stuff
>>
>>     $query = '..something in here..';
>>
>>     //..do more stuff
>>   }
>>}
>>
>>To use this we would just do:
>>
>>$object = new first_class();
>>$object->main();
>>
>>BUT! We need ot change the query. We shouldn't hack the code everytime
>>the query changes because it's not nice. So, instead of making a hook we
>>do three things:
>>1. Move the query to a function
>>2. create a reference variable in the class and point it to $this
>>3. ask the reference variable for the query.
>>
>>That is:
>>class first_class {
>>   var $_this = null;
>>
>>   function first_class() {
>>      $this->_this = &$this;
>>   }
>>
>>   function main() {
>>     //...do stuff
>>
>>     $query = $this->_this->query();
>>
>>     //..do more stuff
>>   }
>>
>>   function query() {
>>     return '..some query goes here...';
>>   }
>>}
>>
>>Which is used just the same way:
>>$object = new first_class();
>>$object->main();
>>
>>NOW... we want to change the query. This is done in two steps:
>>1. create a second class with the function "query" in it
>>2. reference $object->_this to our new class's object
>>
>>function second_class {
>>   function query() {
>>     return '...a different query...';
>>   }
>>}
>>
>>This is used by doing:
>>
>>$object = new first_class();
>>$queryObject = new second_class();
>>$object->_this = &$queryObject;
>>$object->main();
>>
>>This query used will be the one in second_class.
>>
>>For most situations, this is idential to hook. But, it's a whole load
>>nicer in many  situations. For example, where a class does a lot of
>>things (e.g. split something, move something, sum something, delete
>>something, then edit something, compare something and then archive it)
>>having to go "if(is_array(...some var))... then call the object..." is
>>boring.
>>
>>This method puts the responsibility for implementing the hook outside
>>the class which does the work. Much nicer!
>>
>>dan
> 
> 
> 




More information about the TYPO3-dev mailing list