【问题标题】:Get the contents of parent class获取父类的内容
【发布时间】:2017-05-14 20:22:36
【问题描述】:

我是面向对象编程的新手。我需要访问子类中抽象父类中生成的 $key 变量的内容。如何在 postAction() 方法中获取 $key 变量的内容?

abstract class ApiControllerAbstract extends Zend_Rest_Controller {

   public function init() {
            $cryptoHelper = new Application_Model_CryptoHelper;
            $pass = '';
            $salt = '';
            $passwordIterations = 100;
            $keySize = 32;
            $iv = "";
            $key = $cryptoHelper->getKey($pass, $salt, $passwordIterations, $keySize);
   }

}

class Api_SubscribersuploadController extends ApiControllerAbstract  {

   public function postAction() {
       // I need to access contents of key variable from here.
       echo parent::$this->key;
   }

}

我尝试使用echo parent::$this->key; 但它没有显示任何内容。

【问题讨论】:

标签: php oop


【解决方案1】:
  abstract class ApiControllerAbstract extends Zend_Rest_Controller {

  public function init() {
        $cryptoHelper = new Application_Model_CryptoHelper;
        $pass = '';
        $salt = '';
        $passwordIterations = 100;
        $keySize = 32;
        $iv = "";
        $key = $cryptoHelper->getKey($pass, $salt, $passwordIterations, $keySize);
        return $key;
}

}

class Api_SubscribersuploadController extends ApiControllerAbstract  {

 public function postAction() {
   // I need to access contents of key variable from here.
$this->init();
 }

}

【讨论】:

  • 一个小提示:$this->init()应该把返回值赋给一个变量,不然作者还是不能访问$key
  • 谢谢。我现在拿到钥匙了。不使用return语句可以访问吗?
  • @Goutam.A.S 不,你不能
猜你喜欢
  • 2015-09-14
  • 1970-01-01
  • 2020-06-16
  • 2011-01-05
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多