将对象从1页移动到另一页? [英] move object from 1 page to another?

查看:131
本文介绍了将对象从1页移动到另一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

干草家伙。我对PHP中的OOP有点新鲜。我学会了如何编写和创建对象。有没有办法将一个对象传递给另一个脚本?无论是使用GET或POST或SESSION或其他。如果没有如何分配一个对象在一个页面上的一些变量,然后分配相同的对象更多的变量在另一个页面?



谢谢

解决方案

您可以在会话中存储对象,但在调用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆