move object from 1 page to another?

后端 未结 5 1981
清歌不尽
清歌不尽 2020-12-28 10:06

Hay guys. I\'m kinda new to OOP in PHP. I\'ve learnt how to write and create objects. Is there a way to take an object and pass it to another script? either using GET or POS

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-28 10:24

    You can store objects in the session but you need to include the file which contains the class definition before calling session_start() (or use class autoloading and set this up before you start the session). For example:

    On every page:

    //include class definition
    require('class.php');
    
    //start session
    session_start();
    

    1st page:

    $object = new class();
    $object->someProperty = 'hello';
    
    //store in session
    $_SESSION['object'] = $object;
    

    Subsequent pages:

    $object = $_SESSION['object'];
    
    //add something else, which will be stored in the session
    $object->anotherPropery = 'Something';
    

提交回复
热议问题