move object from 1 page to another?(将对象从一页移动到另一页?)
问题描述
伙计们.我对 PHP 中的 OOP 有点陌生.我已经学会了如何编写和创建对象.有没有办法获取一个对象并将其传递给另一个脚本?使用 GET 或 POST 或 SESSION 或其他.如果没有,我将如何在一个页面上为对象分配一些变量,然后在另一页上为同一对象分配更多变量?
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?
谢谢
推荐答案
您可以在会话中存储对象,但您需要在调用 session_start() 之前包含包含类定义的文件(或使用 类自动加载 并在开始会话之前进行设置).例如:
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:
在每一页上:
//include class definition
require('class.php');
//start session
session_start();
第一页:
$object = new class();
$object->someProperty = 'hello';
//store in session
$_SESSION['object'] = $object;
后续页面:
$object = $_SESSION['object'];
//add something else, which will be stored in the session
$object->anotherPropery = 'Something';
这篇关于将对象从一页移动到另一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将对象从一页移动到另一页?
基础教程推荐
- php中的PDF导出 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- php中的foreach复选框POST 2021-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
