将对象从1页移动到另一页? [英] move object from 1 page to another?
问题描述
谢谢
您可以在会话中存储对象,但在调用session_start()之前需要包含包含类定义的文件(或者使用 class autoloading 并在启动会话之前进行设置)。例如:
在每一页上:
//包含类定义
require('class.php');
//启动会话
session_start();
第一页:
$ object = new class();
$ object-> someProperty ='hello';
//在会话中存储
$ _SESSION ['object'] = $ object;
后续页面:
$ object = $ _SESSION ['object'];
//添加其他内容,这些内容将存储在会话中
$ object-> anotherPropery ='Something';
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 POST or SESSION or whatever. If there isn't how would i assign an object some variables on one page, then assign the same object more variables on another page?
Thanks
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';
这篇关于将对象从1页移动到另一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!