Home  >  Article  >  Backend Development  >  Why can't subclasses inherit from parent classes?

Why can't subclasses inherit from parent classes?

WBOY
WBOYOriginal
2016-12-01 01:27:181580browse

Outputecho session('name')Nothing is displayed. I use {$Think.session.name}in the test template and it cannot be output. Why is this?

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

<?php
namespace Home\Controller;
use Think\Controller;
class TestController extends CommonController{
   public function test(){
        echo session('name');
        $this->display();
    }
}
?></code>

Reply content:

Outputecho session('name')Nothing is displayed. I use {$Think.session.name}in the test template and it cannot be output. Why is this?

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

<?php
namespace Home\Controller;
use Think\Controller;
class TestController extends CommonController{
   public function test(){
        echo session('name');
        $this->display();
    }
}
?></code>

I remember that the initialization method is _ (underscore)

The initialization method is an _ (underscore). I misunderstood it. If the subclass has an initialization function, you must indicate whether the initialization of the parent class is executed. If the initialization of the parent class is not executed automatically.

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