Home > Backend Development > PHP Tutorial > Why can't a subclass use the session of the parent class after inheriting it?

Why can't a subclass use the session of the parent class after inheriting it?

WBOY
Release: 2023-03-02 13:00:02
Original
1157 people have browsed it

After the parent class sets the session, why does the subclass echo session('name'); display nothing after inheriting the parent class?

<code><?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller{
   public function __initialize(){
         session('name','lili');
   }
}
?>

<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends CommonController{
    public function user(){
            echo session('name');
    }
}
</code>
Copy after login
Copy after login

Reply content:

After the parent class sets the session, why does the subclass echo session('name'); display nothing after inheriting the parent class?

<code><?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller{
   public function __initialize(){
         session('name','lili');
   }
}
?>

<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends CommonController{
    public function user(){
            echo session('name');
    }
}
</code>
Copy after login
Copy after login

<code><?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller{
   public function __initialize(){
         session('name','lili');
   }
}
?>

<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends CommonController{
   public function __initialize(){
         parent::__initialize();
   }


    
    public function user(){
            echo session('name');
    }
}</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template