Home >Backend Development >PHP Tutorial >Why can't a subclass use the session of the parent class after inheriting 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>
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>