[Typo3-dev] callUserFunction and getUserObj (was: Hooks...)

Martin Poelstra martin at beryllium.net
Mon Dec 13 15:01:14 CET 2004


Hi,

> I think it is good to have both ways, maybe we have to give better
> explanations how both work for the implementor of a hook. I didn't think

There's something you should be aware of when using callUserFunction and 
getUserObj.
Let's assume you have the following situation:

class tx_myclass {
  var $something = 0;

  function foo($params,$ref) {
    $this->something = 1;
  }

  function bar($params,$ref) {
    $this->something++;
  }
}

Now, it is apparent from this situation that the 'state' of the class needs 
to be preserved. This can of course be done with

$obj =& t3lib_div::getUserObj('&tx_myclass'); // Note the ampersands!
$obj->foo($params,$ref);
$obj->bar($params,$ref);

or with e.g.

t3lib_div::callUserFunction('&tx_myclass->foo',$params,$ref);
t3lib_div::callUserFunction('&tx_myclass->bar',$params,$ref);

Both methods use a 'pool' of classes that need to be remembered (i.e. are 
prefixed with an ampersand), but these pools are stored in different arrays, 
so you can't simply mix them!
So e.g.:

$obj =& t3lib_div::getUserObj('&tx_myclass');
$obj->foo($params,$ref);
t3lib_div::callUserFunction('&tx_myclass->bar',$params,$ref);

will have another result ($something will now be 1, instead of 2).
It is perfectly possible to mix the use of 'stored' and 'non-stored' classes 
though, e.g.:

$obj1 =& t3lib_div::getUserObj('&tx_myclass');
$obj2 =& t3lib_div::getUserObj('tx_myclass');
$obj3 =& t3lib_div::getUserObj('&tx_myclass');

Now, $obj1 will be the same as $obj3, while $obj2 is a newly created object 
(not stored). The same holds for callUserFunction.

I hope this clarifies things a little...

Grtz,
Martin

----- Original Message ----- 
From: "Robert Lemke" <robert at typo3.org>
Newsgroups: typo3.dev
To: <typo3-dev at lists.netfielders.de>
Sent: Monday, December 13, 2004 1:33 PM
Subject: Re: [Typo3-dev] Hooks...


> Hi Julle,
>
> Christian Jul Jensen wrote:
>> In roberts tutorial I read something like 'some like the callUserObj
>> way better because it follows some fancy design pattern', this must
>> have to be singleton.
>
> yes it is.
>
>> Now the thing is, you can also call objects and have them instantiated
>> as singletons with the callUserFunction, in fact the only thing I can
>> see that is not equal or better with the callUserFunction is that you
>> can not define a clear signature of the method called from within the
>> hook, but have to pass everything as an array.
>
> yes, that is the main difference.
>
>> On the other hand you have the benefit of not having to specify the
>> methodname in the hook, thereby providing a higher level of
>> transparency, in fact the client can decide if it wants to call a
>> function or method on an object.
>
> using the callUserObj way let's you choose a function / method name,
> getUserObj works with predefined method names which are expected to exist
> in your class.
>
> Generally it is a good idea to user the getUserObj way if you have more 
> than
> one function you need to provide for the hook, because you only have to
> specify your class name once and all functions of that hook will be used
> automatically.
>
> I prefer the getUserObj way where it makes sense, but sometimes the
> callUserFunction way is quite more flexible.
>
>> If not I think we should stick to one way of doing hooks, it's not
>> like we donøt have enough complexy in TYPO3 already, in fact I would
>> also like to see some kind of abstraction like
>> t3lib_extmgm::registerHook()
>
> much about an abstraction function, but at the first glance I can't 
> imagine
> how that function would look like. Do you?
>
> -- 
> robert
> _______________________________________________
> Typo3-dev mailing list
> Typo3-dev at lists.netfielders.de
> http://lists.netfielders.de/cgi-bin/mailman/listinfo/typo3-dev
> 





More information about the TYPO3-dev mailing list