1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How do I make global variables in my classes?

Discussion in 'XenForo Development Discussions' started by Jaxel, Oct 12, 2010.

  1. Jaxel

    Jaxel Well-Known Member

    I'm new to Object Oriented Programming... I've always been more procedural. But for now I'm trying to make some global variables. I have a class, with about 15 functions within it. At the top of all 15 of those functions, I am declaring several variables. Redundant code pisses me off, so I'm trying to figure out how to declare and use those variables globally within the function... What I've tried so far doesn't work...

    This is a cut of my class: (a lot of code has been removed to make this neater)
    Code:
    class EWRporta_ControllerPublic_Wiki extends XenForo_ControllerPublic_Abstract
    {
    	public $perms;
    
    	public function actionIndex()
    	{
    		$this->perms = $this->getPermissions();
    
    		echo "Index: ";
    		print_r($this->perms);
    
    		return $this->responseReroute(__CLASS__, 'Wiki');
    	}
    
    	public function actionWiki()
    	{
    		echo "<br />Wiki: ";
    		print_r($this->perms);
    	}
    }


    Ideally, what this prints out should be:
    Code:
    Index: Array ( [view] => 1 [edit] => 1 [create] => 1 [delete] => 1 [admin] => 1 )
    Wiki: Array ( [view] => 1 [edit] => 1 [create] => 1 [delete] => 1 [admin] => 1 )

    However, it only prints out:
    Code:
    Index: Array ( [view] => 1 [edit] => 1 [create] => 1 [delete] => 1 [admin] => 1 )
    Wiki:
    As you can clearly see, I'm either not globally writing to the public $perms variable, or I'm not accessing it correctly. Does anyone know what I'm doing wrong?
     
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    Try:

    Code:
    class EWRporta_ControllerPublic_Wiki extends XenForo_ControllerPublic_Abstract
    {
    	public $perms;
    
    	public function EWRporta_ControllerPublic_Wiki()
    	{
    		$this->perms = $this->getPermissions();
    	}
    	public function actionIndex()
    	{
    		//$this->perms = $this->getPermissions();
    
    		echo "Index: ";
    		print_r($this->perms);
    
    		return $this->responseReroute(__CLASS__, 'Wiki');
    	}
    
    	public function actionWiki()
    	{
    		echo "<br />Wiki: ";
    		print_r($this->perms);
    	}
    }
     
  3. ragtek

    ragtek Guest

    pfui


    shame on jeremy he copied this:p
    PHP:
        public function EWRporta_ControllerPublic_Wiki()
    should be
    PHP:
        public function __construct()
     
  4. Jaxel

    Jaxel Well-Known Member

    I just tried that... also tried "public function __construct()" and it gives me the following error:

    Fatal error: Call to a member function getMinorSection() on a non-object in /library/EWRporta/ControllerPublic/Wiki.php on line 11

    Code:
    	public function __construct()
    	{
    		$this->perms = $this->getPermissions();
    		$this->slugs = explode('/', $this->_routeMatch->getMinorSection());
    	}
     
  5. Jeremy

    Jeremy XenForo Moderator Staff Member

    I've never used __construct() in any of my classes.
     
  6. Indigo

    Indigo Active Member

    PHP:
    public function __construct($request$response$routeMatch)
    {
        
    parent::__construct($request$response$routeMatch);
        
    $this->perms $this->getPermissions();
        
    $this->slugs explode('/'$this->_routeMatch->getMinorSection());
    }
    Also, all the cool kids have been using __construct since PHP5 came out 6 years ago. :p
     
  7. Jeremy

    Jeremy XenForo Moderator Staff Member

    Well, I'm cool, but I've never had any formal programming training so... lol. I should start using __construct();.
     
  8. Jaxel

    Jaxel Well-Known Member

    That did it! Thanks Indigo...
     
  9. Mike

    Mike XenForo Developer Staff Member

    Rerouting goes through the whole process again. You get a new controller object.

    You can either stick it in the registry (XenForo_Application::set/get) or put it in a static member with the controller (which would then be accessed via self::$name).
     
  10. Jaxel

    Jaxel Well-Known Member

    Okay... getting back to this... I have discovered an issue... Using this code as an example:

    Code:
    <?php
    
    class EWRporta_ControllerPublic_Test extends XenForo_ControllerPublic_Abstract
    {
    	public $userInfo;
    
    	public function actionIndex()
    	{
    		print_r($this->userInfo);
    		return $this->responseReroute(__CLASS__, 'View');
    	}
    
    	public function actionView()
    	{
    		print_r($this->userInfo);
    		exit;
    	}
    
    	public function __construct($request, $response, $routeMatch)
    	{
    		parent::__construct($request, $response, $routeMatch);
    
    		$this->userInfo = XenForo_Visitor::getInstance()->toArray();
    	}
    }
    Theoretically, this should work, and should print out:
    Code:
    Array
    (
        [user_id] => 1
        [username] => Jaxel
    	... etc ...
    )
    Array
    (
        [user_id] => 1
        [username] => Jaxel
    	... etc ...
    )
    However, instead it prints out:
    Code:
    Array
    (
        [user_id] => 0
        [username] =>
    	... etc ...
    )
    Array
    (
        [user_id] => 1
        [username] => Jaxel
    	... etc ...
    )
    For some reason, the Visitor instance is always showing up as an unregistered user on the actionIndex page using the __construct. Is there a solution to this?
     
  11. Jaxel

    Jaxel Well-Known Member

    BTW... this is my real construct:

    Code:
    	public function __construct($request, $response, $routeMatch)
    	{
    		parent::__construct($request, $response, $routeMatch);
    
    		$this->perms = $this->getModelFromCache('EWRporta_Model_Perms')->getPermissions();
    		$this->slugs = explode('/', $this->_routeMatch->getMinorSection());
    	}
     
  12. Mike

    Mike XenForo Developer Staff Member

    The session hasn't be initialized in the constructor yet. At least on the first call (before the reroute).

    Try sticking your call in _preDispatch($action) - you can create/override that function with your class. Do call parent::_preDispatch($action) though.
     
  13. Jaxel

    Jaxel Well-Known Member

    So you're saying I should change it to: ?

    Code:
    	public function _preDispatch($action)
    	{
    		parent::_preDispatch($action);
    
    		$this->perms = $this->getModelFromCache('EWRporta_Model_Perms')->getPermissions();
    		$this->slugs = explode('/', $this->_routeMatch->getMinorSection());
    	}
     
  14. Mike

    Mike XenForo Developer Staff Member

    Yes, I believe so.
     
  15. Jaxel

    Jaxel Well-Known Member

    this works great:
    Code:
    	public function _preDispatch($action)
    	{
    		parent::_preDispatch($action);
    
    		$this->perms = $this->getModelFromCache('EWRporta_Model_Perms')->getPermissions();
    		$this->slugs = explode('/', $this->_routeMatch->getMinorSection());
    	}
    Can I remove the "parent::_preDispatch($action);" line? It doesn't appear to do anything, and I can't see any problems after I remove it.
     
  16. Mike

    Mike XenForo Developer Staff Member

    No, you should always keep it in. There's no guarantee that behaviors won't be added. (And indeed, they could be added by a modification.)
     

Share This Page