You are here: Re: how it works? pass a function as array(&$sess_handler, 'open') instead of '$sess_handler->open' « All PHP « IT news, forums, messages
Re: how it works? pass a function as array(&$sess_handler, 'open') instead of '$sess_handler->open'

Posted by Sjoerd on 04/17/06 14:42

> 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.

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация