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

Why scripts shouldn't be written in ONE PHP FILE!

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
ilir
Smarty Rookie


Joined: 20 Apr 2003
Posts: 16
Location: NYC

PostPosted: Thu May 08, 2003 12:58 am    Post subject: Why scripts shouldn't be written in ONE PHP FILE! Reply with quote

Hi,


I started a project from scratch using Smarty, PHP, and Pear (for DB abstraction and Date calculations). My design involved only ONE PHP FILE that would be central to the entire project and that would call other template files as needed. In the design sheet, this plan seemed awesome and, from my point of view, it appeared to be easier to maintain an entire portal-like website from a single PHP file than to have 10 or 20 PHP files.

So, I continued development and within 2 weeks it was completed. Now, however, an unplanned problem occured: THE WEBSITE WAS SLOW because that one single PHP file had grown 1,500 lines of code (55kb) Rolling Eyes I tried to use caching but it would not work well with another class that I have (a class that is quite important to site navigation).

Although late in the game, I am thinking of slicing my PHP file into smaller bits and hope that it will improve performance on the front-end. If anyone wishes to go the same route as I did, please make sure your website is going to be ~500 lines of code because it'll cause you time and headache otherwise.

I hope to have helped someone out there Smile


Ilir.
_________________
The real problem is not whether machines think but whether men do.
- B. F. Skinner
Back to top
View user's profile Send private message MSN Messenger
boots
Administrator


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

PostPosted: Thu May 08, 2003 2:55 am    Post subject: Reply with quote

Try installing a PHP cache. It might not only save you from having to slice your code (which isn't a bad idea, though), it will speed up Smarty and your other libs as well.

See:


This is not at all the same as Smarty's cache and should address your issue pretty well.
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Thu May 08, 2003 12:40 pm    Post subject: Reply with quote

Not to forget PHP Accelerator @ http://www.php-accelerator.co.uk/
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu May 08, 2003 1:23 pm    Post subject: Reply with quote

Thanks andre -- Actually, I was mistaken, it is ionCube's PHP Accelerator and not APC powering Yahoo!
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Thu May 08, 2003 6:01 pm    Post subject: Reply with quote

... and PHPA is a little faster Smile
Back to top
View user's profile Send private message
ilir
Smarty Rookie


Joined: 20 Apr 2003
Posts: 16
Location: NYC

PostPosted: Thu May 08, 2003 8:03 pm    Post subject: Reply with quote

I already have Zend Optimizer, though. I read that it conflicts with PHPA somehow. Is this true? Do you guys have these two running together?


Thanks a lot.

Ilir.
_________________
The real problem is not whether machines think but whether men do.
- B. F. Skinner
Back to top
View user's profile Send private message MSN Messenger
boots
Administrator


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

PostPosted: Thu May 08, 2003 8:20 pm    Post subject: Reply with quote

IMHO, Zend Optimizer is mostly unneeded, particularly if you have PHPA. I don't think I'm the only one that thinks this Wink

PHPA will definately beat the pants off of the Optimizer, so it is worth the switch Wink
Back to top
View user's profile Send private message
sweatje
Smarty Regular


Joined: 17 Apr 2003
Posts: 70
Location: Bettendorf, Iowa, USA

PostPosted: Thu May 08, 2003 8:38 pm    Post subject: Reply with quote

There is a specific conflict with Zend Optimizer (at least it was listed in the docs when I installed). PHPA does far more than the Optimizer, so you are best of removing it.
_________________
Jason
jsweat_php AT yahoo DOT com
Back to top
View user's profile Send private message
Squee-D
Smarty n00b


Joined: 24 Jun 2003
Posts: 4
Location: Waipu, New Zealand

PostPosted: Tue Jun 24, 2003 7:43 am    Post subject: Can't help myself Reply with quote

<EDIT>
My bad, message that follows from boots shows i wasn't paying attention and that i'm wrong about includes. I had to write a tester to see of course, and its true, you can include a class inside another class and even run the constructor immediately thereafter.. I don't know why I had thought this wasnt possible
</EDIT>

I cant help myself, all my apps in php use a single entry point AND a base 'god-class'. The god class is simply to remove myself from the world of 'global' in functions and keep all code looking the same ( '$this->' everywhere).

On the topic of a single entry point (one .php file for every part of the site) the biggest drawback is caused by include/require etc.. These functions won't work inside a class - so what i have done, and is a little horrifying, is to assertain the list of required includes early on in the constructor of my 'god class' - with a modular DB driven site, this isnt so hard to do.

After i have my list, i actually end the constructor, 'include' all the modules i need and then run $godclass->resume_after_include() to run the application proper.

Its a bit of a hack, but i find it a lot more preferable to multiple entry points into the application code (different .php files)


Last edited by Squee-D on Tue Jun 24, 2003 8:13 am; edited 1 time in total
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 Jun 24, 2003 8:01 am    Post subject: Reply with quote

Hi Squee-D.

A nit-pick: Single entry point is not the same as a single PHP file for all of the site code Smile

Also, what do you mean that include/require won't work inside a class? Do you mean to dynamically build a class with includes? You're right about that. But include/require work fine from within a class method.

Sounds interesting.
Back to top
View user's profile Send private message
JonBoy
Smarty Rookie


Joined: 09 Oct 2003
Posts: 6
Location: Madison, Wisconsin, USA

PostPosted: Thu Oct 09, 2003 6:43 pm    Post subject: I find myself using Smarty like an ASP.NET coder... Reply with quote

In ASP.NET you override the base PAGE class and largely populate individual pages with instances of "fat-code" modules. (This allows you to stick an identical list of hot topics on two different pages, for example.) The use of a single "aspx" file to handle everything is discouraged. Each "fat-code" module itself consists of a XML-ish template and a separate VB/C# "code behind" file.

(That said, I collect most of my paychecks from an ASP.NET application in which a single "aspx" processes about 80% of all requests and a second processes about 20% more.)

Anyway, when I work with smarty, I find myself coding up pages in a similar fashion. For example, my "home.php" will contain a number of calls like the following example. In each "module" I have a *.php "code behind" file and a *tpl Smarty-based template.

Code:
include('j_header.php')
$j_header_array = j_header_makearray()
$smarty->display('j_header.tpl')


Anyway, this seems to work for me so far (with caching it gets really cool)...but it means I am not using a single PHP file as my "entry point".

Idea (As a rule of thumb, it's usually not a good idea to have lengthy, runtime-interpreted ANYTHINGS, anyway.)
Back to top
View user's profile Send private message
ItalicBold
Smarty n00b


Joined: 10 Oct 2003
Posts: 1

PostPosted: Fri Oct 10, 2003 7:13 am    Post subject: Reply with quote

Hi, I havent realy read the whole thread, but I am just interested in exactly why this is running so slowly. Is it because of the size of your file or the fact that everything is run through this file.

What confuses me is that I have a fairly large site running without caching and it runs extremely fast. Also are the smarty files themselves not rather large, and wouldnt these be processed with everypage aswell?
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 -> Tips and Tricks 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