PHP Optimization Tips

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I wrote a database access wrapper a while ago and made it public. Someone emailed me and asked me why I didn't use any PHP Optimization Techniques such as making some functions withing a class static (At least that is how I understood what he said... he said "i do not understand why have you not followed some php memory optimization techniques like using static things etc.")

I would like to learn some of those techniques... I never really bothered with memory optimizations because all of my scripts were relatively quick. Any tips?

Thank you.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I found some interesting tips,

dev.virtuemart.net/cb/wiki/4162

http://www.chazzuka.com/blog/?p=58
Moderator Remark: replaced dead link with link to apparent original source
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Start here -- http://www.php.net/manual/en/oop5.intro.php

Towards the top-right of every page there is a link to the next section, for instance in the page I linked to the links label is "The Basics>>".

Starting with the introduction, read every page those links take you to, all the way to the one labeled "Object Serialization".

Don't skip around. If you see links to other sections in a page, ignore the links. If you don't quite understand something and one of the links looks like it will explain it, ignore it anyway. After you finish with a page, go back and collect the links you were tempted to click on and save them.

Once you've gone through every page, go through your list of links and read the pages those links point to. Only read those pages though, ignore any links in them.

After you've gone through all of your links, go back to that OOP introduction and read through the whole section again.
  • Nightslyr
  • Proficient
  • Proficient
  • Nightslyr
  • Posts: 283

Post 3+ Months Ago

Bogey wrote:
I wrote a database access wrapper a while ago and made it public. Someone emailed me and asked me why I didn't use any PHP Optimization Techniques such as making some functions withing a class static (At least that is how I understood what he said... he said "i do not understand why have you not followed some php memory optimization techniques like using static things etc.")

I would like to learn some of those techniques... I never really bothered with memory optimizations because all of my scripts were relatively quick. Any tips?

Thank you.


To be honest, if your scripts are running fast, I wouldn't worry about it. If they start to slow, your best bet is to use some caching (memcache, APC, etc.) as it has a far more noticeable impact than the other little tweaks mentioned in the second post in the thread.

To be honest, I can't believe the link said not to use __autoload. That (or the equivalent spl functions) is a cornerstone for any well-managed OOP PHP project. It's certainly better than trying to manually keep track of your includes.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

I want to address a few things in that list now that I look at it, too.

Quote:
Set the maxvalue for your for-loops before and not in the loop.


I don't completely agree with this. I see no problem with the following.

PHP Code: [ Select ]
for($i = 0, $toi = count($eyes); $i < $toi; $i++)


Quote:
Avoid magic like ... __autoload


Nonsense. There's a little trick with __autoload where you place the definitions for classes that are likely to be used in combination with the autoloaded class so that it still works like loading a library of functions via include, but also keeps the awesomeness that is __autoload.

Quote:
Use full paths in includes and requires, less time spent on resolving the OS paths.


The reason you should do this has more to do with knowing where you are on the filesystem and always using the intended file than it does speed.

Quote:
If you need to find out the time when the script started executing, $_SERVER['REQUEST_TIME'] is preferred to time()


Be sure to take into consideration the "auto_globals_jit" setting on your server when thinking about this. I'll leave it up to you to figure out what that setting is for. :)

Quote:
Turn on apache's mod_deflate


Has absolutely nothing to do with PHP. To say this optimizes PHP is like saying a NASCAR racer is a better driver because his car is 100 pounds lighter than it was before.

Quote:
A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts.


I can not stress how much I agree with this enough. Most pages that people make with PHP don't change very often.

I actually use a cache folder where I have PHP save a copy of the page using the REQUEST_URI that mod_rewrite ended up turning into a traditional URL before PHP got ahold of the request.

Subsequent requests to that URI use mod_rewrite to check that cache folder and rewrite the URI to any existing static file found in there before turning it over to PHP if it doesn't find anything.

Here's a little bit about combining that with gzip.

Quote:
mod_gzip which is available as an Apache module compresses your data on the fly and can reduce the data to transfer up to 80%


Same thing I said about mod_deflate. Also, be sure to take a look at the link I posted a second ago.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Awesome! Thank you. I will cache the results and implement a lot of those tips to the class. It would be a lot better :D

I really appreciate the help... more tips would be awesome!
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

SpooF wrote:
I found some interesting tips,

http://dev.virtuemart.net/cb/wiki/4162

By the way... this page doesn't exist anymore
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Dug up the cached copy at Google and found what looks like a link to the original article. I'm not sure though. If anyone knows be sure to let a moderator know. :)
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

SpooF wrote:
I found some interesting tips,

dev.virtuemart.net/cb/wiki/4162

http://www.chazzuka.com/blog/?p=58

Thanks for that update. I would try to optimize my code as much as I can as soon as I get some free time.

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 77 guests
  • 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 post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.