Jump to content

Use of globals is forbidden


Recommended Posts



I am not able to use global variable in my module ,it gives error in prestashop validator as "Use of globals is forbidden"


I am using global $smarty;global $cookie, $_LANGADM; and prestashop code files used global $_MODULES;


I am getting blank screen when global is remove from smarty variable.


Following giving blank screen

$this->smarty->assign('MYTEXT_TEXT', $mymodule);
return $this->smarty->display(__FILE__, './views/templates/front/mymodule.tpl');

Please help on using smarty and other variable without global



Edited by edge82 (see edit history)

Share this post

Link to post
Share on other sites

$_MODULE and $_LANGADM variables are global variables in core PS files


How to use these variable using this and context. I am getting following error 


Variable $_MODULE is undefined.
Variable $_LANGADM is undefined.


Please advise.

Share this post

Link to post
Share on other sites

There are many one example is classes\module\Module.php from version 6

	 * This function is used to determine the module name
	 * of an AdminTab which belongs to a module, in order to keep translation
	 * related to a module in its directory (instead of $_LANGADM)
	 * @param mixed $currentClass the
	 * @return boolean|string if the class belongs to a module, will return the module name. Otherwise, return false.
	public static function getModuleNameFromClass($currentClass)
		// Module can now define AdminTab keeping the module translations method,
		// i.e. in modules/[module name]/[iso_code].php
		if (!isset(self::$classInModule[$currentClass]) && class_exists($currentClass))
			global $_MODULES;
			$_MODULE = array();
			$reflectionClass = new ReflectionClass($currentClass);
			$filePath = realpath($reflectionClass->getFileName());
			$realpathModuleDir = realpath(_PS_MODULE_DIR_);
			if (substr(realpath($filePath), 0, strlen($realpathModuleDir)) == $realpathModuleDir)
				// For controllers in module/controllers path
				if (basename(dirname(dirname($filePath))) == 'controllers')
					self::$classInModule[$currentClass] = basename(dirname(dirname(dirname($filePath))));
				// For old AdminTab controllers
					self::$classInModule[$currentClass] = substr(dirname($filePath), strlen($realpathModuleDir) + 1);

				$file = _PS_MODULE_DIR_.self::$classInModule[$currentClass].'/'.Context::getContext()->language->iso_code.'.php';
				if (Tools::file_exists_cache($file) && include_once($file))
					$_MODULES = !empty($_MODULES) ? array_merge($_MODULES, $_MODULE) : $_MODULE;
				self::$classInModule[$currentClass] = false;

		// return name of the module, or false
		return self::$classInModule[$currentClass];

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More