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
WBOYOriginal
2016-12-01 01:27:451160browse

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>

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>

<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>
Statement:
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