简体   繁体   English

如何从子类访问父属性? PHP

[英]How do I access parent property from subclass? PHP

I have an issue accessing top level variables from sub-level class.我在从子级类访问顶级变量时遇到问题。 Here's an example...这是一个例子...

Application.php:应用程序.php:

class Application {
    var $config;
    var $db;

    function __construct() {
        include_once('Configuration.php');
        include_once('Database.php');
        $this->config   = new Configuration;
        $this->db       = new Database;
    }
}

Configuration.php:配置.php:

class Configuration {
    var $dbhost = 'localhost';
}

Database.php:数据库.php:

class Database {
    function __construct() {
        echo parent::config->dbhost;
    }
}

It is clear to me that usage of parent is wrong here as the subclass does not extend the parent class, but how do I access it?我很清楚,这里使用parent是错误的,因为子类没有扩展父类,但我如何访问它?

Thank you.谢谢你。

You should create a Base class that in its construct creates a $db link.您应该创建一个在其构造中创建$db链接的Base类。 Then let all classes that require database access extend that class.然后让所有需要数据库访问的类扩展该类。 Your nomenclature here with "parent class" is incorrect.您在此处使用“父类”的命名法不正确。

class Base {
   private $db;   // Make it read-only

   function __construct() {
      $this->db = DB::connect();    // It's a good practice making this method static
   }

   function __get($property) {
      return $this->$property;
   }
}

class Application {
    public $config;

    function __construct() {
        parent::__construct();

        require_once 'Configuration.php';
        require_once 'Database.php';
        $this->config   = new Configuration();
    }

    function random_function() {
       $this->db(....)    // Has full access to the $db link
    }
}

The parent notation is used to access the parent of the object in the object hierarchy.父符号用于访问对象层次结构中对象的父对象。 What you are doing here is trying to get at the caller, not the parent你在这里做的是试图找到来电者,而不是父母

The way that you would do this is to pass in an instance of your configuration to the database object.您这样做的方法是将您的配置实例传递给数据库对象。

    class Database {
          protected $config;

          public function __construct(Configuration $config){
                $this->config = $config;
          }

          public function connect(){
                //use properties like $this->config->username to establish your connection. 
          }
    }

The parent notation is used when you extend a class and make a child class to call methods on the parent .当您扩展类并使子类调用父类上的方法时,将使用父表示法。

    class MySuperCoolDatabase extends Database {
          protected $is_awesome; 

          public function __construct(Configuration $config){
               // do all the normal database config stuff
               parent::__construct($config);
               // make it awesome
               $this->is_awesome = true;
          }
    }

This defines a child class, which is a type definition that serves the same role as the base class with a slightly different implementation.这定义了一个子类,它是一个类型定义,其作用与基类相同,但实现略有不同。 Instances of this can still be said to be a Database.... just a different kinds of database.这样的实例仍然可以说是一个数据库......只是一种不同类型的数据库。

Well, although I think that Orangepills answer is better.好吧,虽然我认为 Orangepills 的答案更好。 If you dont want to use it and since all variables are public, you could simply pass the variable like this:如果您不想使用它并且由于所有变量都是公开的,您可以简单地传递这样的变量:

class Application {
    var $config;
    var $db;

    function __construct() {
        include_once('Configuration.php');
        include_once('Database.php');
        $this->config   = new Configuration;
        $this->db       = new Database($this->config->dbhost);
    }
}

class Configuration {
    var $dbhost = 'localhost';
}

class Database {
    function __construct($dbhost) {
        echo $dbhost;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM