Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

extend class and assign vars - why doesn't this work?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
GDTRFB
Smarty n00b


Joined: 24 Sep 2003
Posts: 3

PostPosted: Wed Sep 24, 2003 7:19 pm    Post subject: extend class and assign vars - why doesn't this work? Reply with quote

What am I missing here?

This is in a generic include file for the PHP code:

Code:

$exampleVariable = "FOO";

class mySmarty extends Smarty{
     function mySmarty(){
          $this->Smarty();

          $this->assign('test', $exampleVariable);
          $this->assign('works', 'This Works');
     }
}


The template looks like this:

Code:

test = {$test}<BR>
works = {$works}


And then calling then creating and using the template

Code:

$test = new mySmarty()
$test->display('template.tpl');


This does not display

FOO<BR>
This Works

The variable "test" is not getting assigned. Why? I've tried enclosing it in single quotes which results in test being assigned as $exampleVariable and in double quotes which has the same effect as no quotes, that is, no assignment.

Thanks!
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Sep 24, 2003 7:50 pm    Post subject: Reply with quote

In PHP, if you want to access a global variable from a function, you must import it into the function's scope using:

global $exampleVariable;

HTH
Back to top
View user's profile Send private message
GDTRFB
Smarty n00b


Joined: 24 Sep 2003
Posts: 3

PostPosted: Thu Sep 25, 2003 1:19 am    Post subject: Reply with quote

Thanks!

Now the followup, using the example above, does $this->assign('test', $exampleVariable); get evaluated and locked in at the time $x = new mySmarty(); is called? Or will the value of the global $exampleVariable be used at the time it is referenced in the template?

$exampleVariable = "apple";

$x = new mySmarty();

$exampleVariable = "banana";

x->display('template.tpl');

what prints for {$test}

??
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Sep 25, 2003 4:01 am    Post subject: Reply with quote

These aren't particularly Smarty related questions, but....the way it is written, "apple" would get printed. You should be able to test that for yourself rather quickly--faster than you can even ask, I would think Smile When you call your Smarty constructor and test gets assigned, the value of $exampleValue is copied into test, so changing $exampleValue later has no effect on test. On-the-other-hand, if you used assign_by_ref() instead of assign, then "banana" would have got printed.

Never-the-less, IMO, you should really, really try to avoid using global variables, particularly in this way.

Cheers!
Back to top
View user's profile Send private message
GDTRFB
Smarty n00b


Joined: 24 Sep 2003
Posts: 3

PostPosted: Sat Sep 27, 2003 1:29 am    Post subject: Reply with quote

boots wrote:

Never-the-less, IMO, you should really, really try to avoid using global variables, particularly in this way.


Yes, you're right, I could have (and did) test it myself to get the answer, but then I wouldn't have gotten the little bit of information mentioned above. Please help me understand why I shouldn't use globals in this way.

In this particular instance I have a set of navigation links and a couple other look-and-feel parameters that are assigned dynamically depending on the users authorization level. The same links appear on all the pages (home, reports, etc) but the actual links behind the places change. It seemed more efficient doing it this way than putting the conditional inside the smarty constructor and hard-coding the variables there.

While I've been coding for years, it's always been procedural. I'm getting a good crash course on OO with Smarty and I can see many of the OO benefits, but I still tend to think procedurally.

Thanks!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP