View Full Version : Functions vs handlers
05-23-2011, 06:18 AM
Is there a significant difference?
I understand functions should be more convinient for repetitive stuff but...
I am creating a seperate include file for every function that I need to accomplish (log in, register, view articles), and its sort of the same thing as using functions as far as I can tell... Am I missing something?
05-23-2011, 06:23 PM
No, PHP doesn't have the concept of handlers since they are triggered by events. Underlying, they are the same - an event triggers a handler which is simply a method to invoke something based on that event. The only real difference is that signatures for handlers are often locked so they cannot be modified, and they are called implicitly by the event instead of explicitly by the code.
There is no problem in PHP with grouping your functions into fewer scripts. Just make them logical, any function manipulating a user can be grouped into a script with other functions that manipulate the user. Don't group the login and view articles together, but the login and the register both seem good to group together.
PHP does not load the function body until its been called for the first time, so the waste is IO time of the file itself + the function signature which is 8 bytes + the size of the arguments (which I would presume all to be 8 bytes in PHP since it doesn't know what other size it is until runtime). This means you needn't concern yourself with the number of functions that are going unused in the current script. Of course, since it is interpreted try to keep it to a reasonable size - 10MB script would require linking every time you include it, even if you only intend to use one function. This adds up your IO time when it needs to read, and will slow it down. A reasonable size would be un-noticeable in this parsing time. I would expect anything over 10K lines would start to become noticeable.
Alternatively, write classes to deal with everything. Just like functions only the shell of the class and methods are loaded until called. OO programming will allow you to simulate 'events' via patterns such as the observer model, so that when one object changes another object can detect and deal with it. This is the closest we can get in PHP, specifically for web development.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.