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

Newbie: Sorry for stupid questions

 
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
Gregg
Smarty Rookie


Joined: 07 Jul 2003
Posts: 6

PostPosted: Mon Jul 07, 2003 10:57 pm    Post subject: Newbie: Sorry for stupid questions Reply with quote

I am fairly new to PHP and VERY new to Smarty so forgive me if my question is stupid.

This is kind of a 2 part question.

1. I have see both include ('Smarty.class.php') and require ('Smarty.class.php'). Is there a difference?

2. Do we need the above statement as well as $smarty = new Smarty; in every .php file we create? Or only in our initial page we load?

Thanks. Sorry again for the stupid questions. Very Happy
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Jul 07, 2003 11:18 pm    Post subject: Reply with quote

1. there is a subtle difference between require and include just in case of an error (file not found or not readable, mostly): "require" raises an error and stops execution of the whole php-script running, where "include" raises a warning and execution is continued with the next command after the "include()"-statement.
in case of no error include and require are pretty much the same.

2. "require 'smarty.class.php'" and "$smarty = new smarty" has to be done *once* per http-request running (that is once per page that is sent to the browser). you don't have to (and should not) do this more than once per page displayed (which may involve more than one php-file, though).

you can prevent multiple inclusion of smarty.class.php by using "require_once" instead of "require" (which is a good habit to do so, with all files containing class-definitions, since the overhead of require_once against require is minimal, but may save you from a fatal "cannot redeclare class..."-error). regarding "$smarty = new smarty" there is no such automatic mechanism: you have to choose a central point in your php-script that does this and that is not called twice. if it was called a 2nd time the changes to $smarty since the first instanciation ("=new..."-thing) are overwritten by a fresh and clean instance of class smarty.

HTH
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Gregg
Smarty Rookie


Joined: 07 Jul 2003
Posts: 6

PostPosted: Tue Jul 08, 2003 2:33 am    Post subject: Reply with quote

OK, good information, thanks. But I would like to clarify just a bit.

"require 'smarty.class.php'" and "$smarty = new smarty" has to be done *once* per http-request running (that is once per page that is sent to the browser).

Do you mean once per page like if I have 4 pages, index.php, bla.php, henry.php, and foo.php and each one of these pages calls another, so like index.php submits to foo.php, etc, I need to have the require and the $smarty = new Smarty;? Is that right?

Thanks for helping me out.

Gregg
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Tue Jul 08, 2003 3:23 am    Post subject: Reply with quote

Hi.

Messju's point was that during the lifetime of the request, you should only require and instance $smarty once. Another way to think of it is by entry point. For any user accessible entry point (ie. index.php, bla.php, etc) $smarty should be instanced only once.

You're question raised a subtle point: if index.php submits to bla.php (via a form or query string--ie via HTML) then this amounts to a new http request -- both index.php and bla.php would have to instantantiate $smarty on their own. In this case, each of index.php and bla.php are separate entry points.

On the-other-hand, if index.php did a PHP include or require of bla.php, then the single entry point is index.php meaning that $smarty should not be instanced in both scripts.

HTH
Back to top
View user's profile Send private message
Gregg
Smarty Rookie


Joined: 07 Jul 2003
Posts: 6

PostPosted: Tue Jul 08, 2003 3:28 am    Post subject: Reply with quote

Perfect, got it. Thanks.
Back to top
View user's profile Send private message Visit poster's website
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