Search code examples
phpoopzend-frameworkzend-formzend-form-element

How to access protected variable in Zend forms?


i am trying to create a dynamic multioption select using dynamic values for a shipping cost pulldown,

the array comes in and creates the select input fine but the protected values are omitted. this makes no sense to me. i even tried using a public getter to access the protected value but it still comes in blank.

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

here is the $form code placed within the form's init function:

    $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));

Solution

  • Since you are not creating instance of ORed_Shipping_LabelFactory hence you cannot use instance variables (variables which starts with $this are instance variables) .

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