views:

141

answers:

3

I'm building a webapp using php, ajax, javascript, mysql. I've been worring about something for a while, but not sure if it's really a problem or not. Here's the basic concept of how the code is working...I would like to know if I should change it now, or if it is o.k to use as is. Performance is important to me...over 5,000 users should be able to use the app without much of a performance problem. Part of my worrying is just paranoia of being self taught developer and not knowing all the best practices out there.

Here's the basic webapp functionality:

user executes a call via the browser through onclick event --> ajax call to phpPG1.php. --> phpPG1.php runs a query against the db and stores the results in array, then includes another php page called HTMLphp.php, instantiates new object from HTMLphp.php page, which is a class --> calles function of the class and passes the array that contains the results of a query-->the class functions builds the HTML table and passes back a string --> the phpPG1.php page sends back the string with the table data to the ajax call which displays the string in the given DIV tag it belongs in.

The HTMLphp.php contains all the functions that are used to return HTML tables for the whole webapp. HTMLphp.php would look something like this:

Class HTML_stuff
{
   function html_TABLE1($results_array)
   {

      $string = 'THE HTML TABLE WITH ITS DATA IN IT'
      return $string
   }

   function html_TABLE2($results_array)
   {

      $string = 'THE HTML TABLE WITH ITS DATA IN IT'
      return $string
   }

}

So, here's my question. the HTMLphp.php page has 5,606 lines of code in it right now, which represents 100 or so functions in the class. Basically, every page in my webapp "includes" this page into it in order to be able to use the class functions to display the html tables. I'm about half way done with the webapp, so there will be a lot more lines of code do add to this file. I'm not exactly up to par on how computers execute code, especially when using class objects, but I understand the basics if "interpretive languages" like php.

I'm wondering if this is a bad idea and if I should do something like this: For each function inside the HTML_stuff class just simply remove the code for each function and place it in it's own separate .php page that gets included like this:

Class HTML_stuff
{
   function html_TABLE1($results_array)
   {
      include_once 'TABLE1.php'; //the html table for this function.
      return $string;
   }

   function html_TABLE2($results_array)
   {
      include_once 'TABLE2.php';
      return $string;
   }
}

My basic assumption is that I only include the HTML needed when I make the call to the particular function, thus reducing the overall size of the HTMLphp.php page assuming this would help in overall site performance...am i in left field with this thinking? Part of me is thinking that this is simply the same as the first option, just organized differently and it would do anything to the overall performance. However, I did read somewhere that fewer "includes" is better for performance.

What do other people do, or are there other best practices on how to do this sort of thing? Is it negligible to worry about this with a site of say 5,000 - 10,000 users?

Thanks.

+2  A: 

You really have over 100 functions in your class?
Probably (no I am 110% sure) your class is not well designed then, especially if it is "only" for HTML table creation.

Object Oriented Programming is a good technique but you have to use it correctly. It sounds to me that you just put all the procedural functions into one place. That is not OOP. And it is hard to maintain the code.

100 methods in one class is way, way too much. Refactor your code, put it into multiple classes and only import those you really need. And even without knowing more about your application I can say that it (and you) will benefit from it.

I cannot let you use such a class with good conscience ;)

You ask what you should do?
Learn OOP in the context of PHP and maybe read something about Design Patterns.

Edit:

I just saw that you have a function to create every HTML in your application, is this correct? If so you really should consider to build your tables dynamically, which will result in less code and is probably easier to maintain.

Felix Kling
I agree, that's why I wanted to put this out there. When you say build the tables dynamically what do you exactly mean? Can you give me a code example of how you would do this and a possible way of implementing it? Each page basically DISPLAY's, EDIT's, CREATE NEW data. Take a contact list for example, the display portion lists all the contacts in a table, the Edit portion lists one of the contacts with input boxes to change the data, the New table is exactly the same as the edit page. Should I create one class for this page with a function to Edit, Display, and create New?
Ronedog
+1  A: 

Including the specific PHP file inside a function/method rather than an open include will definitely help things as only the needed items will get included and interpreted. But the PHP intrepreter will still need to go through those 5000 lines code of that huge class, which IMHO is a really bad way to go about it, and will be a major bottleneck.

In reality, 5000-10000 users is not a whole lot, but it again depends on how the usage is going to be. If all of them could be logged in simultaneously, and shooting out requests at your server (something like 10k requests per minute or 166 req/s approx.) and there's little or no caching involved, then it could be a serious bottleneck but that again depends on a lot of factors. Instead, a good way to go about it would be to use some sort of load testing tool like ab or JMeter and find it out for real.

If the results don't look so good after doing these tests, then find out what the bottleneck is. You could use APC or Memcache to implement caching, or various other ways to improve performance. But taking a wild shot in the dark, I'd say that 5k line class is something that you should consider splitting, not just for performance reasons but also for a good design. And perhaps, if the logic of building those HTML pieces is not too complex, you could offload that to the client by sending the data as JSON/XML, and letting Javascript construct the table using this data.

Anurag
I looked at offloading the building of the tables to the client, but the logic is complex enough, that I call multiple php class objects to put various parts of data inside the table, I really couldn't see how I could have the client do it instead. The other problem, is the table columns will change a lot from result-set to result-set, so creating one class that can be reused over and over that simply outputs a table based on the input, I can't see how to do this when the columns are always changing.
Ronedog
If the data is available as an array of objects, then it's simply a matter of looping through values, and creating each row. Libraries like jQuery and MooTools are great in simplifying DOM creation. But like you said, since the html logic is complex, it might be too much work to offload it to the client.
Anurag
A: 

Problem solved. I changed to code to only read in the function that was needed at the time of the call. Now it reads in a few hundred lines instead of 5,000.

Ronedog