2

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

2
  • Except for very limited cases, global variables should typically be avoided (too long to get into in a comment...). Instead, maybe you should be referring back to the front controller to get the data. (Though without knowing your exact situation I may be giving you bad advice.) Commented Nov 20, 2011 at 11:26
  • @Corbin...I modify and briefly describe my question. Please check it and help me Commented Nov 20, 2011 at 13:29

3 Answers 3

6

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');
Sign up to request clarification or add additional context in comments.

3 Comments

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
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?
The link you mentioned about Zend_Registry is not working
0

Looks to me like you are making two distinct requests calling, respectively, checkusernameAction() and subscribeAction(). Since these are distinct requests, the email value you set in the controller during checkusernameAction() will be gone on the second request which calls subscribeAction(). It's the same controller class, but you are looking at two distinct instances, one in each request.

As I see it, you can either:

  1. Pass the email address in each AJAX request, but this seems unlikely since you get the email address from the first call to checkusernameAction().

  2. Save the email in the session during the first checkusernameAction() call and then pick it up during the second subscribeAction() call.

  3. Extract the "get email from username" code into a separate class or method and then call it in both places. After all, you don't want to get bitten by a "race condition" in which the state of the system changes between the two AJAX requests (maybe the user's email changes via some other process or via another set of requests that occur after the first AJAX request containing the call to checkusernameAction().

Comments

0

You can also used a function for set and get a value.

// Setter function
public function setConsumerKey($key)
{
    $this->_consumerKey = $key;
    return $this;
}

// Getter function

public function getConsumerKey()
{
    return $this->_consumerKey;
}

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.