Set and get global variable in php (ZendFramework)

11,445

You can use Zend_Registry to use the variable throughout application.

You can set a variable like this

Zend_Registry::set('email', $EMAIL);

and later can get it like this

$email= Zend_Registry::get('email');
Share:
11,445
Amritpal singh
Author by

Amritpal singh

PHP Developer

Updated on June 04, 2022

Comments

  • Amritpal singh
    Amritpal singh almost 2 years

    I am using ZendFramework with PHP, and I want to set and get a variable as a global variable. I.E. I set it in the class of Zend Controller and access it any action in the class.

    For example:

    <?php
    
    class SubscriptionController extends Zend_Controller_Action
    {
        private $EMAIL = false;
        private $USERNAME = false;
    

    First I am validating email addres with ajax call

        public function checkusernameAction()
        {
            $email = //query to find email;     
            if($email){
            $EMAIL = true;
            }else{
            $EMAIL = false;
            }
        }
    

    then I want subscribe user on the basis of private variable again with ajax call

        public function subscribeAction
        {
          if($EMAIL == true)
           {
             //some stuff 
           }        
        }
    

    I am getting private var by $this->EMAIL, but not able to access it

  • Amritpal singh
    Amritpal singh over 12 years
    Thanx buddy.... this is the good option but I am facing a problem i.e I initialize the registry in init() with ::set('email' false) and again declaring it in other action that I am calling with ajax as ::set('email', true), and then checking it in third function as true or false and everytime i am getting false. Is there any other option to do the same
  • James Butler
    James Butler over 12 years
    I'm not sure if it will ease your life or not, but you could pass round a config object in the registry which just handles a subset of configuration options?
  • Viuu -a
    Viuu -a over 6 years
    This is to access variable inside a class..But I want to access variable from different controllers and models.Like a catalyst object in catalyst framework
  • Viuu -a
    Viuu -a over 6 years
    The link you mentioned about Zend_Registry is not working