简体   繁体   中英

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:

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:

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

Database.php:

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

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

Thank you.

You should create a Base class that in its construct creates a $db link. 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. 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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