View previous topic :: View next topic |
Author |
Message |
ilir Smarty Rookie
Joined: 20 Apr 2003 Posts: 16 Location: NYC
|
Posted: Thu May 08, 2003 12:58 am Post subject: Why scripts shouldn't be written in ONE PHP FILE! |
|
|
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) 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
Ilir. _________________ The real problem is not whether machines think but whether men do.
- B. F. Skinner |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 08, 2003 2:55 am Post subject: |
|
|
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 |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 08, 2003 1:23 pm Post subject: |
|
|
Thanks andre -- Actually, I was mistaken, it is ionCube's PHP Accelerator and not APC powering Yahoo! |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Thu May 08, 2003 6:01 pm Post subject: |
|
|
... and PHPA is a little faster |
|
Back to top |
|
ilir Smarty Rookie
Joined: 20 Apr 2003 Posts: 16 Location: NYC
|
Posted: Thu May 08, 2003 8:03 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 08, 2003 8:20 pm Post subject: |
|
|
IMHO, Zend Optimizer is mostly unneeded, particularly if you have PHPA. I don't think I'm the only one that thinks this
PHPA will definately beat the pants off of the Optimizer, so it is worth the switch |
|
Back to top |
|
sweatje Smarty Regular
Joined: 17 Apr 2003 Posts: 70 Location: Bettendorf, Iowa, USA
|
Posted: Thu May 08, 2003 8:38 pm Post subject: |
|
|
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 |
|
Squee-D Smarty n00b
Joined: 24 Jun 2003 Posts: 4 Location: Waipu, New Zealand
|
Posted: Tue Jun 24, 2003 7:43 am Post subject: Can't help myself |
|
|
<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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Jun 24, 2003 8:01 am Post subject: |
|
|
Hi Squee-D.
A nit-pick: Single entry point is not the same as a single PHP file for all of the site code
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 |
|
JonBoy Smarty Rookie
Joined: 09 Oct 2003 Posts: 6 Location: Madison, Wisconsin, USA
|
Posted: Thu Oct 09, 2003 6:43 pm Post subject: I find myself using Smarty like an ASP.NET coder... |
|
|
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".
(As a rule of thumb, it's usually not a good idea to have lengthy, runtime-interpreted ANYTHINGS, anyway.) |
|
Back to top |
|
ItalicBold Smarty n00b
Joined: 10 Oct 2003 Posts: 1
|
Posted: Fri Oct 10, 2003 7:13 am Post subject: |
|
|
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 |
|
|