【问题标题】:How do I access parent property from subclass? PHP如何从子类访问父属性? PHP
【发布时间】:2013-06-03 19:56:18
【问题描述】:

我在从子级别访问顶级变量时遇到问题。 这是一个例子......

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;
    }
}

配置.php:

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

数据库.php:

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

我很清楚,这里使用 parent 是错误的,因为子类没有扩展父类,但是我如何访问它?

谢谢。

【问题讨论】:

    标签: php class variables parent


    【解决方案1】:

    您应该创建一个Base 类,该类在其构造中创建一个$db 链接。然后让所有需要数据库访问的类扩展该类。您在此处使用“父类”的命名不正确。

    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
        }
    }
    

    【讨论】:

    • 这将使我的主文件中有更多代码。 codeinclude('Base.php');包括('Application.php');...code
    • silkfire,我不知道你是怎么写的,但就个人而言,我更喜欢在课堂之外尽可能少地编写代码,并且尽可能少地编写代码,从而节省我的时间并更快地编写代码。此外,代码越多 - 服务器负载就越多。不过可能不是你的论据。
    • 代码多是可以的,只要干净、有效、易读即可。
    【解决方案2】:

    父表示法用于访问对象层次结构中对象的父级。您在这里所做的是试图联系呼叫者,而不是父母

    执行此操作的方法是将配置实例传递给数据库对象。

        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. 
              }
        }
    

    当你扩展一个类并让一个子类调用父类的方法时使用父表示法。

        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;
              }
        }
    

    这定义了一个子类,它是一个类型定义,其作用与基类相同,但实现略有不同。这个实例仍然可以说是数据库....只是不同类型的数据库。

    【讨论】:

    • 据我了解,在这种情况下,总共将有两个 Configuration 类对象实例 - 一个在 Application 类中,一个在 Database 类中。这种方式需要两倍以上的服务器资源来运行脚本?这个比较简单,但是脚本比较复杂,不能这样浪费服务器资源。
    • 这不是真的......唯一的方法是使用 new 关键字创建两个配置对象。这将创建对同一配置对象的两个引用。
    • 这种将对象传递给另一个对象的方法称为构造函数依赖注入,它使您的数据库类更具可重用性,因为现在您的数据库类未绑定到任何特定的配置实现。
    【解决方案3】:

    好吧,虽然我认为 Orangepills 的答案更好。如果您不想使用它,并且由于所有变量都是公开的,您可以像这样简单地传递变量:

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      相关资源
      最近更新 更多