如何在Zend表单中访问受保护的变量?

时间:2012-04-03 03:39:27

标签: php oop zend-framework zend-form zend-form-element

我正在尝试使用动态值创建动态多选项选项以用于运费成本下拉,

数组进入并创建精选输入,但省略了受保护的值。这对我没有意义。我甚至尝试使用公共getter来访问受保护的值,但它仍然是空白的。

        protected $_regular     = 4.95;
        protected $_oneDay      = 14.95;
        protected $_twoDay      = 14.95;

        public function getShippingOpts(){

            return array(
                "regular"=>"Regular 5-7 Business Days $".$this->_regular,
                "two-day"=>"Express 3-4 Business Days $".$this->_twoDay,
                "one-day"=>"Overnight 1-2 Business Days $".$this->_oneDay
            );
        }

这里是表单的init函数中的$ form代码:

    $shType = new Zend_Form_Element_Radio("sh_type");
    $shType->setLabel("Please select a type of shipping")
            ->setAttrib('class', 'co-shipping-type')
            ->setRequired(true)
    ->setMultiOptions(ORed_Shipping_LabelFactory::getShippingOpts());
    $shTypeToSubmit = new Zend_Form_Element_Hidden('speed');
    $shipping2->addElements(array($shType, $shTypeToSubmit));

1 个答案:

答案 0 :(得分:0)

由于您没有创建ORed_Shipping_LabelFactory的实例,因此您无法使用实例变量(以$开头的变量是实例变量)。

   static $_regular     = 4.95;
            static $_oneDay      = 14.95;
            static $_twoDay      = 14.95;

            public static function getShippingOpts(){

                return array(
                    "regular"=>"Regular 5-7 Business Days $".self::$_regular,
                    "two-day"=>"Express 3-4 Business Days $". self::$_twoDay,
                    "one-day"=>"Overnight 1-2 Business Days $". self::$_oneDay
                );
            }