Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-如何扩展和访问父构造函数属性_Php_Constructor_Class Visibility - Fatal编程技术网

PHP-如何扩展和访问父构造函数属性

PHP-如何扩展和访问父构造函数属性,php,constructor,class-visibility,Php,Constructor,Class Visibility,我正在尝试访问扩展它的子类中的父类uu构造属性,但是不确定如何执行此操作,因为我尝试了多个方法,但没有给出预期的结果 所以我有一个baseController和一个扩展它的indexController,我希望能够直接访问子控制器中父控制器的属性 $config = ['site' => 'test.com']; class baseController { public function __constr

我正在尝试访问扩展它的子类中的父类uu构造属性,但是不确定如何执行此操作,因为我尝试了多个方法,但没有给出预期的结果

所以我有一个baseController和一个扩展它的indexController,我希望能够直接访问子控制器中父控制器的属性

            $config = ['site' => 'test.com'];

            class baseController {

                public function __construct($config){

                    $this->config = $config;

                }

            }

            class indexController extends baseController {

                public function __construct(){
                    parent::__construct(); // doesnt seem to give any outcome
                }

                public static function index() {

                    var_dump($this->config); // need to access within this method

                }

            }

            $app->route('/',array('indexController','index')); // the route / would call this controller and method to return a response

这里的代码有几个问题。您正在将配置设置为全局配置,它应该位于
BaseController
内,并将其设置为
public
protected

class BaseController {
  protected $config = ...
正如@mhvvzmak1所提到的,您的子构造函数正确地调用了父构造函数。例如,您可以这样做:

 class IndexController extends BaseController {

     public function __construct(){
         $config = [];
         parent::__construct($config);
     }
最后,正如dan08提到的,您不能从静态方法引用
$this
,请更改索引函数:

public function index() {

更新

如果确实希望子函数按照框架的要求保持静态,那么可以在
BaseController
上将config设置为静态函数,并在子函数中调用它

class BaseController {

   protected static function config() {
     return ['site' => 'mySite'];
   }
}

class Child extends BaseController {
   public static function index() {
      $config = BaseController::config();
   }
}

如果在静态上下文中调用
indexController::index
,则无法访问
$this
\\构造($config)与\\构造()不同,否则如何访问该属性,使用static:::$config?我认为我需要将index方法作为静态调用,作为我使用的框架的一部分。我以前尝试过u构造($config),但仍然没有得到任何结果,因此将其删除,因为我不确定它为什么不工作“与其他方法不同,PHP在u构造()“框架似乎不允许我从静态更改方法,我得到一个错误”非静态方法indexController::index()不应静态调用(8192)“您自己调用索引函数吗?或者框架会为您调用它吗?当我访问站点route/时,框架会为我这样做,它会自动在indexController中查找“index”方法,并显示函数的结果,因为这就是我分配给此特定路由的结果:$app->route('/',array('indexController','index'));你能展示一下你是如何在路由器上实现这一点的吗?还有这方面的文档吗?只是浏览了一下网站上的文档,没有提到将控制器与路由器一起使用。有一点关于它的使用,但它是非常少,很难错过,请参阅:。类问候语{public static function hello(){echo'hello world!';}}Flight::route('/',array('Greeting','hello');