Protected by Copyscape DMCA Takedown Notice Infringement Search Tool
All opinions expressed on this blog are my own and do not reflect those of BIET Jhansi students and employees,staff,or any official whatsoever, colleagues, family or friends.I express my opinions as a free citizen of a democracy exercising my Fundamental Right of speech. The intention of this blog is merely to air my views and opinions (and sometimes, frustration) and is not intended to insult, instigate,disgrace or hurt anyone(body,organisation or institution). Anyone is free to disagree with any or all of my views and can express them here or elsewhere. Any civil dialogue that is not unreasonably hurtful is welcome. I, however, reserve the right to delete any comment without any reason or warning.No content of this blog will in any way be a violation UNDER IPC Sections 506 and 295A .Legal issues if any will be ristricted to the MEERUT jurisdiction only.This blog/web space is in the process of being copyrighted to safegaurd my interests erstwhile this be considered to be under the creative commons commercial INDIA License.This space resorts to politically and ethically correct statements, complying with the spirit of blogging .This is an opinion medium, not a reporting medium and hence should not be IN ANY CASE BE TAKEN AS A FUNCTION OF MAINSTREAM MEDIA.The blog complies with the NAAVI guidelines. Thank you, MANOJ SINGH RANA

Monday, October 5, 2009

Dynamic code generation in PHP

As in most scripting languages, you can dynamically generate code in PHP. As a sidenote: the reason why it is so simple to implement dynamic code in scripting languages is that you already have the "eval" function (it is called with the script), all you have to do is to provide an interface to it from the language.

Usually you wouldn't want to use this feature (since it adds complexity and can create security risks), however there might be exceptions. For example you might consider using this method if you need to write feature-rich code executed in a tight loop. Take a look at my script for creating gradients to see what I'm talking about.

There I had code which needed to support 3 color spaces and possibly will be called in a tight loop (to generate every pixel of an image for example - although I wouldn't recommend that). The fact that the effective function got dynamically compiled avoided having to execute an "if" at every call (depending on the colorspace), because only the relevant code was included.

Alternatively I could have written three separate implementation of the function (depending on the color space) and note in an instance variable which one needs to be used. This achieves the same performance but gives more "compile time" syntax checking:

class ColorGradient {
function calcRGB(...) { ... }
function calcYUV(...) { ... }
function calcHSV(...) { ... }

function getColorArray(...) {
return call_user_func(array('ColorGradient ', $this->calcFuction), ...);
}
}

In conclusion: with all the recent (and not so recent) additions PHP is getting a very powerful scripting language for the web.

No comments:

Post a Comment

Comments Section