-1

我试图让一个子类(提要)从主类(控制器)继承一个变量。

我有 3 个文件。它们看起来如下。

核心文件:

/*  ----------------------------- *
 *  LOAD THE CONTROLLER CLASS NOW *
 *  ----------------------------- */
 require(CORE_PATH . 'controller.php');

 require(CORE_PATH . 'load_controller.php');

控制器类:

<?php

class Controller {

    var $sessions, $a;

    public function __construct() {
        $this->sessions = new Sessions();
        $this->a = 'yo';
    }
}

饲料类:

class Feeds extends Controller {
    public function feeds() {
        /* We can load other resources here */

    }

    function index($val) {
        echo $this->a;
    }
}

修复:

<?php require(PROTECT);

class Controller {

    protected $variable = "Setting the variable right here works.";

    public function __construct() {
        $this->variable = "Setting it right here will only work for the current object. It's as if variable isn't being set at all when it comes to other classes.";
    }
}
4

1 回答 1

0

除非您使用 PHP4,否则您应该避免使用var. 利用public, private, protected

在您的情况下,因为您已经使用var了您的成员变量$a,所以应该可以从子类访问。

你有任何错误吗?

于 2013-05-09T07:11:57.370 回答