Storing A PHP Object In A Session Variable

前端 未结 3 805
無奈伤痛
無奈伤痛 2021-01-18 19:14

I\'m new to OOP and am writing one of my first classes. I work for an insurance broker and am trying to use a Class to store things about a quote, and store the Object as a

3条回答
  •  清歌不尽
    2021-01-18 19:42

    Make sure that either the class definition is present before session_start() is called, e.g.

    require_once 'class.MyClass.php';
    session_start();
    

    or set an unserialize_callback_func that will try to load the class definition as described at http://docs.php.net/function.unserialize.

    edit: this can also be done via spl_autoload_register(). E.g.

    spl_autoload_register(function($name) {
        // only a demo ...this might be insecure ;-)
      require_once 'class.'.$name.'.php';
    });
    session_start();
    echo '
    ';
    var_dump($_SESSION);
    echo '
    ';

提交回复
热议问题