How do I make global variables in my classes?

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

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


shame on jeremy he copied this:P
PHP:
	public function EWRporta_ControllerPublic_Wiki()
should be
PHP:
	public function __construct()
 
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());
	}
 
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
 
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
Well, I'm cool, but I've never had any formal programming training so... lol. I should start using __construct();.
 
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).
 
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?
 
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());
	}
 
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.
 
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());
	}
 
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.
 
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.)
 
Back
Top Bottom