doue2666 2014-04-26 09:13
浏览 28
已采纳

无法在同一个类中输出受保护的变量

I have this 2 classes:

class EventScripts {
protected $database;
private $test;

public function __construct(Database $database) {
    $this->database = $database;
}

public function ScriptA($callTime, $params) {
    // Does NOT output the information of the database. It simply does nothing and the code aborts.
    var_dump($this->database);
    return $params;
}   
}

class EventSystem {

protected $database;
protected static $results;

public function __construct(Database $database) {
    $this->database = $database;
}


public function handleEvents() {

    // outputs the array with information of the database
    var_dump($this->database);

    $EventScripts = new EventScripts($this->database);
    $EventScripts->ScriptA(5, "test");          
}
}

I call EventSystem like this:

try {
    $database = new Database("host", "user", "password", "database");
    $EventSystem = new EventSystem($database);
} catch (Exception $e) {
    echo $e->getMessage();
}

$EventSystem->handleEvents();

Now the var_dump() in EventSystem correctly shows me the information of the database that is saved in the protected $database-variable.

But when I do this exact thing in the ScriptA()-method, nothing happens and the code aborts. It even doesn't return anything anymore.

Where is my mistake?

  • 写回答

1条回答 默认 最新

  • donglie7268 2014-04-26 09:24
    关注

    Protected members of classes are available to the class and inherited classes of it. Not to entirely different classes.

    In you can you can extend both classes from one (perhaps abstract) class that is "with database" and it has that details encapsulated as a protected member.

    abstract class DatabaseBased
    {
        /**
         * @var Database
         */
        protected $database;
    
        public function __construct(Database $database)
        {
            $this->setDatabase($database);
        }
    
        protected function setDatabase(Database $database)
        {
            $this->database = $database;
        }
    }
    

    Here the one class you had problems with:

    class EventScripts extends DatabaseBased
    {
        private $test;
    
        public function ScriptA($callTime, $params)
        {
            var_dump($this->database);
    
            return $params;
        }
    }
    

    When you now create the other object, you can inject the database directly:

    public function handleEvents() {
    
        // outputs the array with information of the database
        $EventScripts = new EventScripts();
        $EventScripts->setDatabase($this->database);
        $EventScripts->ScriptA(5, "test");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料