Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-05-2012, 11:18 AM   PM User | #1
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
PHP Classes & Performance

Hi ya, I have a general question about using PHP classes. Imagine I make a large file with many classes, but I may not necessarily need all those classes every time that file is called. Will I suffer a performance loss for classes that I don't initialize? I would expect PHP to look inside abstract or static classes as it loads them, but I'm not sure about the others. Can classes effectively hide code until used? ... Can anything?

Last edited by Custard7A; 11-05-2012 at 05:11 PM.. Reason: Resolved
Custard7A is offline   Reply With Quote
Old 11-05-2012, 01:46 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yep, you'll see a performance loss doing this. PHP will parse all of these files and build the symbols table as necessary to include the definition of any function or class. So it will cost you time and memory to do this.
I don't understand by what you mean saying to hide code. You can write nested functions in which they will not index their signatures until the outer function is called. It still parses the entire file though, so you need to write it proper, but the function will not be available until the outer is called. PHP doesn't support class or method nesting though.

Use an autoload to specify paths and common class extension instead. Throw that onto the php load path and it will automatically find the class when called. Mucho easier with namespacing as it resolves the namespace as a path, so you can simply autoload the extension and then issue a register to the root path. When you issue a use command it will chain the namespace as if it were a filepath.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Custard7A (11-05-2012)
Old 11-05-2012, 04:35 PM   PM User | #3
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
Thanks a lot Fou-Lu, this is surely something I will keep in mind when structuring my files. When I said "hiding code", I meant having code in a file without it effecting the memory use or page load. I also have a curiosity on what you said here: ".. It still parses the entire file though, so you need to write it proper.." I can write a fatal error in a conditional statement, like so..

PHP Code:
 if(== 2) { undefinedFunction(); } 
.. and this won't be a problem. How does this happen, if everything in the file gets parsed? Would this actually be "hiding code" as I mentioned it? Thanks again, all this is very helpful for me.
Custard7A is offline   Reply With Quote
Old 11-05-2012, 04:42 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
runtime error <> syntactical error. If you run that code, it will parse as its valid PHP code, but it will throw a runtime error if it ever enters the condition since undefinedFunction isn't defined (assuming that its not defined of course). It needs to be proper PHP code that doesn't throw a syntactical error so it has to pass the compile and link checks. PHP doesn't have a mechanism to verify that a function exists during link, its only during run that will trigger an error.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Custard7A (11-05-2012)
Old 11-05-2012, 05:11 PM   PM User | #5
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
Ah, I understand that now, <?php if(1 == 2) { ) } ?> kicks the bucket.

Unfortunately I can't use namespaces, since the server I'm on uses PHP 5.2.17. I suppose autoloading is still a good idea, I'll be getting my head around that shortly! Gracias Fou-Lu.

Last edited by Custard7A; 11-05-2012 at 05:17 PM..
Custard7A is offline   Reply With Quote
Old 11-06-2012, 07:12 AM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,855
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
tip: use spl_autoload_register(), it’s way better than __autoload().
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 11-06-2012, 09:30 AM   PM User | #7
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
Quote:
Originally Posted by Dormilich View Post
tip: use spl_autoload_register(), it’s way better than __autoload().
I don't understand how on first glance. I appreciate you making that a link though.
Custard7A is offline   Reply With Quote
Old 11-06-2012, 09:51 AM   PM User | #8
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,855
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
__autoload() usually looks in one place (unless you write a lot of code), spl_autoload_register() allows for several functions, that can look up one individual place (and the look-up stops naturally, if a match is found). and of course you can unregister look-up functions again.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 11-06-2012, 01:57 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Thats right, you effectively chain together functionality of multiple autoloads. So it checks in method a first, then in method b, and all the way through the chain until it either finds a valid condition (such as a class to load), or fails to find it.
This is why the namespaces are so convenient, its a matter of putting it to the root directory and autoloading it:
PHP Code:
set_include_path(get_include_path() . PATH_SEPARATOR realpath(__DIR__ "/../"));
spl_autoload_extensions(".class.php");
spl_autoload_register(); 
The use is automatically invoked to use the filepath as the namespace path, so you never need to issue an include/require again for a class to load. Too bad your PHP version is < 5.3.
Fou-Lu is offline   Reply With Quote
Old 11-07-2012, 01:55 PM   PM User | #10
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
.. you never need to issue an include/require again for a class to load..
Ooh, I like that. Now you've got me imagining all new levels of clean and efficient code. Shouldn't I be able to do that rather easily, even without namespaces?

What if I do this in auto_prepend_file:
PHP Code:

 
// Assuming I'm keeping all my class files at my include path.

  
function __autoload($class) { include $class.'.class.php'; } 
Pardon my use of __autoload() over spl_autoload_register(). I'm still reading about that one, and I didn't want to use it wrong in such a simple example!

Is there a reason to have it more complicated than that?
Custard7A is offline   Reply With Quote
Old 11-07-2012, 02:26 PM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
That would work fine yeah. The problem is that you need to put all the class files in the same place for that to work, but if you were to keep adding to the path, then it would be capable of searching on the path to find the classes it needs.
I wouldn't include it though. I would require_once it.
Fou-Lu is offline   Reply With Quote
Old 11-07-2012, 04:50 PM   PM User | #12
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
I expect spl_autoload_register() will be of help for searching more than one directory. I will use require_once as well.

I'm wondering, do you know if auto_prepend_file uses _once functionality? I might be better to include_once this code in my pages, rather than have it reload every-time it creates a new class.
Custard7A is offline   Reply With Quote
Old 11-07-2012, 05:02 PM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The documentation isn't specific. It indicates it uses include for the file that you want to include, but since its implicitly done before the main, I'd expect that subsequent inclusions do not apply an auto_prepend or auto_append functionality for each one. So it should only do it once.
I myself still use require_once functionality instead of prepending. That's a personal choice though as I like testing each and every class with as minimal dependency as possible, so I point all of them to a common global include, which takes care of my registrations.
Fou-Lu is offline   Reply With Quote
Old 11-07-2012, 06:31 PM   PM User | #14
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
In the section for core php.ini directives it says it uses require, however, it's actually require_once!

I prepended this file:

PHP Code:


<?php if(isset($Var)) { ++$Var; } else { $Var 0; } ?>
I could require or include any number of files, but the only way I could make $Var = "1" was to include or require itself, but not include_once or require_once.
This goes to prove that it is registering that file on the "been included/required" list. As for being require, it fatal errors when the file doesn't exist.

I'll feel a lot better about using it now that I understand that. Anyway, thanks very much Fou-Lu. Your guidance has taught me things I will consider essential in future endeavors.
Custard7A is offline   Reply With Quote
Reply

Bookmarks

Tags
classes, performance

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:30 PM.


Advertisement
Log in to turn off these ads.