Re: [phplib-dev] Re: [phplib] PHPlib session using PHP4 sessions implementation: the code From: Teodor Cimpoesu (teo <email protected>)
Date: 12/04/00

Hi Max!
On Mon, 04 Dec 2000, Max Derkachev wrote:

> > > should make a basic Session class, and then make extensions to it, covering
> > > standard php4 storage modules and the user module, which uses custom
> > > containers.
> > I love this approach, yap. Design custom classes, from simple session
> > management, with a backward compatible API (as the one in the repository right
> > now), and then extend that class to enhance the functionalities.
> >
>
> The problem with it is that the handlers for session_set_save_handler()
> ought to be simple functions, not class methods. the function takes only
> string arguments, so we have to use wrappers for class methods outside
> the class, and session_set_save_handler() should refer to those
> wrappers. And, because of this, the task could not be done only by
> constructing Session_* inheritance chain.

Well, it's true that the way Sascha wrote this wonderful extension to PHP4
was in his style, namely `structured' :) but I guess he forethought one can
wrap all those function into an OO style interface. So there should be a
solution out there (Note: coding hints follow below).

> We need a php function similar to xml_set_object(), say, named
> session_set_object( string name_of_sess_object), that could bind session
> engine to an object, and, hence, arguments for
> session_set_save_handler() would be interpreted as that object's class
> methods, similarly to the xml php module.

I dunno, I had a look at this function and looks neat :).
Also, I am biased for the was the session works in Java (Servlet API),
I love it and use it in the same manner. And despite it's bad publicity,
I found Request/Response objects quite handy; note also Zope and ASP have
them. Anyway, a matter of eggs taste :) [oo in romanian would sound the
same as 'eggs' :) ]

We can propose alternative style, why not. Let's make an /experimental/
dir there and let ppl say what they think about it.

> > The start as I see it is settling on a session class API( a Session Interface,
> > to say so).
> >
> It has existed before and well documented in the PHPLib docs :). Do we
> have to redesign the whole class? It could break the compatibility with
> apps that was built using PHPLib and PHP3.

I sow a lot of doubtful methods, and those were what I was thinking about
when I wrote the above phrase. I surely know about the session's register()
is_registered() and delete() but what else do we use from there as related
to session? umm, maybe hidden_session().

And now, to the proposal for workaround of `wrappers to methods'.

o The simplest would be to plea Sascha to add support for method calls, LOL.
  somehow like call_user_method() ?

o If he is too busy hacking away, then we can use what Zeev though to be
a cool idea: here is the hat, and here is the `create_function()'!
And surprise, it returns the function name, exactly what session_set...()
expects!

scratch:

class Session {
        ...

        function set_custom_session () {
                $this->save_hooks = array();
                $hooks = array('open' => '$save_path,$session_name',
                                           'close' => '',
                                           'read' => '$id',
                                           'write' => '$id,$session_data',
                                           'destroy' => '$id',
                                           'gc' => '$maxlifetime');

                $num_args = func_num_args();
                
                for ($i = 0 ; $i < $num_args ; ++$i) {
                        list ($func,$args) = each ($hooks);
                        $this->save_hooks[$func] = create_function ($args, func_get_arg($i));
                }
        }
        // and eventually, one can set them individually,in a different meth.,I dunno

        function activate_custom_session () {
                session_set_save_handler ($this->save_hooks['open'],
                                                              $this->save_hooks['close'],
                                                              $this->save_hooks['read'],
                                                              $this->save_hooks['write'],
                                                              $this->save_hooks['destroy'],
                                                              $this->save_hooks['gc']);
        }
        //this activates the new handlers
}

so, how would one actually register a new session handler?

[my_super_dooper_session_handlers.inc]

$open =<<<ENDFUNC
  global $sess_save_path, $sess_session_name;

  $sess_save_path = $save_path;
  $sess_session_name = $session_name;
  return(true);
ENDFUNC;

$close=<<<ENDFUNC
        return (true);
ENDFUNC;
 ....
// so on

[my_hackish_script.php]
include_once 'my_super_dooper_session_handlers.inc';
....
$sess->set_custom_session ($open,$close, ...);
$sess->activate_custom_session();

We can have set_custom_session() take file names which contains the
implementation, or we can pass a FunctionFactory() to activate_custom_handlers
just like xml_set_object() and make it return the code from the requested
handler.

So on. There point was to show an idea of implementation.
I would rush and code it right away but unfortunatelly I don't owe
a car and it's the time of the last subway train :)

What do you think?

cheers,

-- teodor

---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-dev-unsubscribe <email protected>
For additional commands, e-mail: phplib-dev-help <email protected>