|
Posted by d on 04/17/06 23:03
"Sjoerd" <sjoerder@gmail.com> wrote in message
news:1145274125.993793.71410@g10g2000cwb.googlegroups.com...
>> session_set_save_handler(array(&$sess_handler, 'open'),
>
> The first parameter would be the object on which to invoke a method.
> The second parameter would be the name of the method ('open').
>
> In session_set_save_handler, when an open occurs, it calls
> $sess_handler->$name_of_method. The method $sess_handler->open is
> actually a callback function.
>
> Without the use of objects, a callback function would be a normal
> function. You would pass the name of the function as a parameter:
>
> <?php
> function foo($functionname) {
> echo $functionname('hello');
> }
>
> foo('strtoupper');
> ?>
>
> As you can see, 'strtoupper' is passed as a parameter and then used as
> a function.
>
> This won't work for objects:
> <?php
> foo('$obj->open');
> foo('Object::Open');
> ?>
>
> The first does not work because the variable $obj is not passed
> correctly. The second way also does not work, but I can not think of a
> good reason why it doesn't.
It's because of the :: - that's part of the language, and not a part of the
variable name. $object::$func would be the equivalent, but that requires
two parameters to pass, whereas using an array means it's one parameter,
which means it's the same for functions and member functions.
> The right way, design-wise, to set a handler is to pass an object which
> implements an interface. The interface specifies which methods an
> object has to have, such as open, close, etc. You then pass an object
> which implements this interface and the methods within this object can
> be called.
>
Navigation:
[Reply to this message]
|