move object from 1 page to another?

后端 未结 5 2002
清歌不尽
清歌不尽 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:32

    Here is an example with autoloading in respect to the answer by Tom Haigh:

    Before you start the session:

    function __autoload($className) {
        $file = PATH_TO_FOLDER_WITH_ALL_CLASS_FILES."/".$className.'.php';
        if(file_exists($file)) {
            require_once $file;
        }
    }
    
    session_start();
    

    Page passing the object:

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

    Page receiving the object:

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

    The autoload method will automatically load the objects while you retrieve data from session.

提交回复
热议问题