php - PHP 将对象从页面传递到套接字
问题描述
我正在使用其功能性 MVC 模型将功能整合到现有平台中。事实是我正在聊天中工作,为此我使用PHP Sockets
我无法更改 MVC 结构中的任何内容,但我需要将socket.php
两个对象放入我的文件中index.php
这些对象包含用户不应该看到的敏感信息。index.php
位于root_folder/subfolder/index.php
和socket.php
位于root_folder/another_subfolder/third_subfolder/socket.php
索引.php
<?php
...
$object1 = new Object1();
$object2 = new Object2();
套接字.php
<?php
...
class Socket extends MyOwnSocketClass{
protected function onmessage($message){
// I need these two objects here
}
}
...
$socket = new Socket($host, $port);
try { $socket->run(); }
catch (Exception $e) { $socket->stdout($e->getMessage()); }
然后,从我php -q root_folder/another_subfolder/third_subfolder/socket.php
用来启动套接字的终端,一切正常,客户端连接没有问题。但与此同时,我试图将这两个对象index.php
从socket.php
...
套接字.php
require '../subfolf/index.php'; // Error failed open stream
require 'root_folder/subfolder/index.php'; // Undefined variable $object1
/*Using sessions*/ $_SESSION['object1']; // Undefined index object1
/*Using globals*/ $_GLOBALS['object1']; // Call to method in null
总之,我不能使用 require,我不能使用 $_SESSION,我不能使用 ../ 上传目录中的级别,如果我在 index.php 中插入套接字,则页面将无限加载。
解决方案
我将假设这index.php
实际上是一个输出内容的页面。在这种情况下,当您使用require()
它时,它会发送其输出,这就是它破坏您的流的原因。
如果你有某种共享对象/类,你应该把它放在它自己的文件中,你可以根据需要将它require()
放在两个文件index.php
中socket.php
。该文件不应该输出任何东西......它应该只包含您需要在两个上下文中包含的 PHP 代码。
推荐阅读
- python-sphinx - Sphinx toctree 指令:如何隐藏标题不被显示?
- reactjs - 如何从 React 中的 JSON 对象中查找最新的 10 个日期元素
- android - 尝试将 AutofillId 类型的对象传递给 Dataset.Builder
- javascript - 如果新邮件已到达我的 gmail,我希望出现一个按钮,并且尽可能多的邮件到达相同的按钮数量
- java - 使用 spring cloud 的多模块 spring 项目的父级
- c - 汇编函数具有相同的名称,后缀为 ..0 和 ..1
- c++ - 根据参数包值定义模板
- postgresql - 如何在 SELECT 中使用 WITH AS(带别名)
- java - Flink 中 java.util.map 和自定义 pojo 的序列化
- c# - 无法从游戏对象的公共数组中获取组件