accessing variable from inside a class

accessing variable from inside a class

am 04.09.2009 23:40:08 von Lars Nielsen

Hi,

How do i access a variable from inside a class? ex. I want to use
$template_dir inside Template.


$template_dir = 'templates/';

class templateParser {
var $output;

function templateParser($templateFile='default_template.htm')
{
(file_exists($template_dir.$templateFile)) ?
$this->output=file_get_contents($template_dir.$templateFile)
:
die('Error:Template file '.$template_dir.$templateFile.' not found');
}

}

I run Fedora 10, apache 2.2.11 and php 5.2.9.
I get an error saying that $template_dir is undefined.

Regards
Lars Nielsen


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: accessing variable from inside a class

am 04.09.2009 23:49:20 von James Colannino

Lars Nielsen wrote:
> Hi,
>
> How do i access a variable from inside a class?

Add the following statement:
global $template_dir;

James

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: accessing variable from inside a class

am 05.09.2009 00:06:07 von Shawn McKenzie

Lars Nielsen wrote:
> Hi,
>
> How do i access a variable from inside a class? ex. I want to use
> $template_dir inside Template.
>
>
> $template_dir = 'templates/';
>
> class templateParser {
> var $output;
>
> function templateParser($templateFile='default_template.htm')
> {
> (file_exists($template_dir.$templateFile)) ?
> $this->output=file_get_contents($template_dir.$templateFile)
> :
> die('Error:Template file '.$template_dir.$templateFile.' not found');
> }
>
> }
>
> I run Fedora 10, apache 2.2.11 and php 5.2.9.
> I get an error saying that $template_dir is undefined.
>
> Regards
> Lars Nielsen
>

Well you have to access it as a global by either declaring it global in
the method (global $template_dir;) or by the superglobal
$GLOBALS['template_dir']. However this is probably not the preferred
way to do it.

I would either define it as a constant define('TEMPLATE_DIR',
'templates/') or you could pass $template_dir into the class constructor
and have it set as a class var, but I prefer the constant.

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: accessing variable from inside a class

am 05.09.2009 00:08:58 von Lars Nielsen

I cant get it to work. I will use a configuration class instead.

function templateParser($templateFile='default_template.htm')
{
$c = new config();
(file_exists($c->template_dir.$templateFile)) ?
$this->output=file_get_contents($c->template_dir.$templateFi le)
:
die('Error:Template file '.$template_dir.$templateFile.' not found');
}


/Lars

fre, 04 09 2009 kl. 14:49 -0700, skrev James Colannino:
> Lars Nielsen wrote:
> > Hi,
> >
> > How do i access a variable from inside a class?
>
> Add the following statement:
> global $template_dir;
>
> James
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php