The Cache: Technology Expert's Forum
Welcome, Guest. Please login or register. September 23, 2019, 11:24:01 AM

Login with username, password and session length

Pages: [1]
Author Topic: classes in php, how they get included  (Read 1966 times)
Offline Offline

Posts: 5627

Back in my day we had 9 planets

View Profile
« on: January 04, 2008, 08:21:01 AM »

I am creating my PHP skeleton and was wondering how I need to do something.

When I include a class, and instantiate it, obviously the code for the class gets loaded and adds its little bit of processing time to the code base of the site.

But what about when I include the class, but never instantiate it on the page that is executing? Does the class use up any processing time (other than the first time if there is any caching going on)?

What if I include dozens of classes that never actually get used on the page? bad, or no effect?

the reason why is because I like using a single include that includes all my others. that way, when I add a new include, I don't need to edit every single page, just the "binder".

make sense?

I could eat a bowl of Alphabet Soup and shit a better argument than that.
Olde World Hacker
Offline Offline

Posts: 10096

View Profile
« Reply #1 on: January 04, 2008, 09:04:26 AM »

Yup. There are actually many different answers to your question.

In all cases there seems to be a "first pass" over whatever you include - if it's horribly corrupt then PHP will cough... but if you have a syntax error halfway down it won't catch it until it actually runs the code... so this leads me to believe that code is never really compiled until it is really used.

include and include_once will bring in a reference to code with no requirement ie., if the file doesn't exist it will continue on.
require and require_once will bring in a reference to the code WITH requirement ie., if it doesn't exist it's a fatal error.

There is, of course, a small penalty for including something regardless of compilation, but if you never use the code I don't think you'll take too much of a hit. The best way, of course, is to use the APC cache so that everything is compiled and pre-linked - you can then include your butt off and take virtually no hit at all. This is how my stuff works because I have lots of classes that I want available all the time.

Another option (if you're worried about it) is conditional inclusion. You can do this:

case 0:
case 1:
case 2:

Note here if the value is 1 then both classA and classB will be included because the switch is not broken. You can also do this:


where in this case a query parameter on the URL is actually specifying what the PHP will include. Clearly not for the faint of heart.

Did you know that you can reference functions the same way?
$buff $functionName()

doSomething() { return 'Hello World'; }

Last but not least is the eval($aCodeBlock) function which will execute the code contained in the aCodeBlock var. That's wonderful, but will not stay compiled in APC so you take a hit for it.

Wow. About 19 million more lines than you asked for. Must be good coffee this morning. Wink

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Offline Offline

Posts: 244

View Profile
« Reply #2 on: January 04, 2008, 05:55:54 PM »

Not directly related to your question, but theres an interesting design pattern I've been trying to use lately described as 'lazy loading' where and object though initialised doesnt really do anything until its called, keeping the memory footprint low, quite useful for DB connections.

Pages: [1]
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks

Valid XHTML 1.0! Valid CSS!