...

View Full Version : referencing global variable in __construct()



anotherJEK
02-24-2012, 11:32 PM
I am writing this code:


$_localMarkup = 'So, what is \'markup\'?';

require_once('WC_php/pixViewerApp.php');

class _PIX_VIEWER extends _VIEW
{

private static $_loaclMarkup = '';

public function __construct()
{
self::$_localMarkup = $_localMarkup; // <<< ??
}

public function getAnswer()
{
return parent::subCLassTest(); //
}

public function getMarkup()
{
return self::$_localMarkup;
}
}

As you can see there is a global (defined in the file) named $_localMarkup.
But the __construct() method will not read this variable.

I initially thought that I could use the syntax
&$_localMarkup;
but that does not seem to work either.

Is there a way, without putting the $_localMarkup variable in a separate file
and including it in the __construct() method?

Thanks for time and attention
JK

cercos
02-25-2012, 12:14 AM
You have to tell the function your using the global $_localMarkup and not $_localMarkup for the function scope.

public function __construct()
global $_localMarkup;
{
self::$_localMarkup = $_localMarkup; // <<< ??
}

and I just saw that:
private static $_loaclMarkup = '';

local is spelled wrong that could be your problem as well or part of your problem.

Fou-Lu
02-25-2012, 01:23 AM
Avoid this whenever possible.
Reformat your constructor to take a $_localMarkup and pass it in during construction. Global is a debugging nightmare and becomes worse in an object oriented environment.

anotherJEK
02-26-2012, 02:52 AM
I saw the misspelling:

private static $_loaclMarkup = '';
// should be
private static $_localMarkup = '';

I have a potential problem with passing this as an argument to the _PIX_VIEWER constructor: the parent class wants a constructor argument
when _PIX_VIEWER is instantiated, so, I am thinking that _PIX_VIEWER
will be faked out by seeing two arguments passed to it.


$_someVar = new _VIEW('argument')
$_anotherVar = new _PIX_VIEWER();
// complaint about _VIEW not getting an argument in the second line
class _VIEW
{ /*various static variable defs*/
public function __construct($_subDep)
{
self::$_subDep = $_subDep;
/* constructor code to set static var values*/
}
}
/*
So, I would expect to instantiate _PIX_VIEWER as
*/
$_yetAnotherVar = new _PIX_VIEWER('argument for parent class', 'argument for child class');
// ????

Thanks again for time and attention
JK



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum