PHP 从父类 CLOSED 访问变量


PHP accessing variables from the parent class CLOSED

我见过一些标题非常相似的问题,但它们与我的特定问题无关。

基本上,我想在一个扩展核心的类中访问我的核心类中的变量,但与其他示例相比,事情似乎相当复杂。我正在使用MVC框架。我简化了下面的代码以删除任何不相关的内容。

索引.php

// Load the core
include_once('core.php');
$core = new Core($uri, $curpath);
$core->loadController('property');

核心.php

class Core
{
    public $uri;
    public $curpath;
    function __construct($uri, $curpath)
    {       
        $this->uri = $uri;
        $this->curpath = $curpath;
    }

    // Load the controller based on the URL
    function loadController($name)
    {       
        //Instantiate the controller
        require_once('controller/'.$name.'.php');
        $controller = new $name();
    }

}

属性.php

class Property extends Core
{
    function __construct()
    {
        print $this->curpath;
    }   
}

打印$this->curpath不会返回任何内容。变量已设置,但它为空。如果我在核心内打印$this>curpath.php它打印得很好。

如何访问此变量?

你做错了tm

  1. 您应该使用自动加载器,而不是手动包含每个类的文件。您应该了解 spl_autoload_register() 和 和命名空间,以及如何使用它们。

  2. 不要在__construct()方法中生成输出。这是一种极其糟糕的做法

  3. 变量仍然存在。这不是问题所在。在 PHP 中,当您扩展时,它不会继承构造函数

  4. 您不了解继承的工作原理。当你在扩展类的实例上调用方法时,在调用扩展类的方法之前,它不会执行父类的方法。它们被覆盖,而不是堆叠。

  5. 不应公开对象变量。您正在破坏封装。相反,og 将它们定义为您应该使用 protectedpublic

  6. 你应该扩展它们的类是不同类型的相同一般的东西。PHP 中的extends表示 is-a。这意味着,当你写class Oak extends Tree时,你的意思是所有的橡树都是树。同样的规则意味着,根据你的理解,所有Property实例都只是Core实例的特例。他们显然不是。

    在OOP中,我们有原则。其中之一是利斯科夫替代原理(简短的解释)。这就是你的课程所违反的。

我认为

问题就在这里:

如果您考虑像这样的简单继承:

class Dog{
    public $color;
    public function __construct($color){
        $this->color = $color;
    }
}
class TrainedDog extends Dog{
    public $tricks;
    public function __construct($color, $tricks){
        $this->tricks = $tricks;
        parent::__construct($color);
    }
}
//Create Dog:
$alfred = new Dog('brown');
//Create TrainedDog
$lassie = new TrainedDog('golden',array('fetch'));

在这个例子中,$alfred是一只棕色的狗,$lassie是一只金色的狗。这两个实例彼此独立,它们唯一的共同点是它们都具有一个名为 $color 的属性。

例如,如果你想要一个在所有 Dog 中都可用的变量,你需要一个类变量:

class Dog{
    public $color;
    public static $numberOfLegs; //Class variable available in every instance of Dog.
    public function __construct($color, $numberOfLegs){
        $this->color = $color;
        self::$numberOfLegs = $numberOfLegs;
    }
}
class TrainedDog extends Dog{
    public $tricks;
    public function __construct($color, $tricks){
        $this->tricks = $tricks;
        parent::__construct($color);
        echo parent::$numberOfLegs;
    }
}

不过,这在许多情况下没有多大意义,因为如果您有两个父类实例(在 Core 中),它们也会共享类变量。

除非可以确保 Core 仅实例化一次,否则此方法将不起作用。如果它只存在一次,您也可以使用常量变量来存储 2 个属性。

如果 Core 存在多个实例/对象,我建议使用组合(如 Alvin Wong 所建议的那样)。

class Core{
      //Just as you programmed it.
}
class Property{
      private $core;
      public function __construct($core){
          $this->core = $core;
          echo $core->curPath;
      }
}

试试这个

include_once('core.php');
$core = new Core('test', 'path');
$core->loadController('property');
class Property extends Core
{
 function __construct($date)
{
    print $date->curpath;
}   
}

class Core
{
public $uri;
public $curpath;
function __construct($uri, $curpath)
{       
    $this->uri = $uri;
    $this->curpath = $curpath;
}

// Load the controller based on the URL
function loadController($name)
{       
    //Instantiate the controller
    require_once($name.'.php');
    $controller = new $name($this);
}

}