简体   繁体   English

为什么session_start()函数在php构造函数中不起作用?

[英]Why does session_start() function does not work in php constructor?

This is my below code 这是我的下面的代码

first.php first.php

class Session
{
   function __construct()
   {
      session_start();
   }
   function start()
   {
      $_SESSION['id'] = "1";
   }
}

second.php second.php

 require "first.php";
 $session = new Session();
 $session->start();
 echo "Session Id ".$_SESSION['id'];

It gives me an error that _SESSION not defined So i have to define session_start in start function and then it worked but i want to use it in constructor!! 它给我一个未定义_SESSION的错误,所以我必须在启动函数中定义session_start,然后它起作用了,但是我想在构造函数中使用它!

我已经在PC(Ubuntu 16.4)上尝试过您的代码,并且可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM