初始化设置值后,从父类继承类属性

时间:2016-03-09 22:14:10

标签: php class cakephp inheritance cakephp-2.8

尝试制作一个允许使用多个支付网关的组件(是的,我意识到之前已经完成)。我创建了一个'PaymentsComponent',以及一些扩展它的特定于网关的组件。

问题是,我无法在父组件中设置/初始化的变量可在子组件中访问。它们是可访问的,但只能使用默认值,而不是初始化后的值。我错过了什么?

在控制器中:

private function processPayment($requestData) {
    $this->Payments = $this->Components->load('Payments');
    $this->Payments->initialize($this);
    debug($this->Payments->apiSecretKey); // shows: '123456789'
    $result = $this->Payments->charge($requestData);
    //...
}

PaymentsComponent

<?php
App::uses('Component', 'Controller');
class PaymentsComponent extends Component {

    public $apiSecretKey = null;

    public function initialize() {
        $this->apiSecretKey = Configure::read('PaymentGateway.secret_key');
    }

    public function charge($data) {
        $result = $this->Authnet->charge($data);
    }
    // ...
}

Authorize.net组件     

class AuthNetComponent extends PaymentsComponent {
    public $components = array('Session');
    public function charge($data) {
        debug($this->apiSecretKey); // shows: null
        // ...
    }
    // ...
}

0 个答案:

没有答案