phpPunk Posted December 21, 2006 Share Posted December 21, 2006 I don't nessecarily mean global in scope...but global is use...and global in namespace in the sense the functionality doesn't fall under any context or namespace other than global or system...For instance, I typically have a function like:[code]function redirect($url) // TODO: Make sure URL is clean or adjust for mod_rewrite, etc header("location: $url"); exit;}[/code]For security and ease of porting my application over from dynamic URL's to clean SEO URLS...Another example would maybe be a random string generator...some might argue that is functionality that belongs in a crypto library...I however see this as a global operation, feeling that a crypto lib should probalby provide it's own (some with more entrophy).So, given the two examples above...I ask...what are some "truely" global operations which you find being reused in your applications...and what is it's purpose?Cheers :) Quote Link to comment https://forums.phpfreaks.com/topic/31443-truely-global-operations/ Share on other sites More sharing options...
utexas_pjm Posted December 21, 2006 Share Posted December 21, 2006 I typically use a Global class and lump all of my utility functions in it which I can then call from a static context. Some examples:[code]<?phpGlobalFuncs::importClass($className);GlobalFuncs::printDebug($dbgString);GlobalFuncs::redirect($url);// etc...?>[/code]I might be trampling over some OO purist paradigms here but I find that it's cleaner to put all of my commonly used utilitarian function under one classname which I can use like a namespace throughout my projects. Quote Link to comment https://forums.phpfreaks.com/topic/31443-truely-global-operations/#findComment-145818 Share on other sites More sharing options...
obsidian Posted December 21, 2006 Share Posted December 21, 2006 utexas_pjm has a good start on what I was going to suggest. When you have a major overarching component of your application that you need to use within different areas, I'd recommend seeing if you can define an abstract class to hold all the functions associated with that component. For instance, to combine the example you gave with what utexas_pjm said, I would try to come up with some sort of abstract SEO class that holds all your functions you would need to use [b]for that purpose[/b] throughout your application.[code]<?phpabstract Class SEO { public function redirect($URL) { // TODO: Make sure URL is clean or adjust for mod_rewrite, etc header("location: $URL"); exit(); }}// Then, whenever you need to call itSEO::redirect($url);?>[/code]This will work with PHP5 only, since PHP4 doesn't fully support OOP and the abstract model. Quote Link to comment https://forums.phpfreaks.com/topic/31443-truely-global-operations/#findComment-145826 Share on other sites More sharing options...
utexas_pjm Posted December 21, 2006 Share Posted December 21, 2006 obsidian,I'm curious as to why you chose to make the class abstract? I come from Java background and in Java you use an abstract class to define a class which is too generic to be instanciated but whose methods should be inhertied by all derived classes. Like the classic example below:[code]<?phpabstract class shape { private $name; public function getArea() { return 0; } public function getName() { return $this->name; }}class rectangle extends shape { private $height; private $length; // ... public getArea() { return $this->length * $this->height; }}?>[/code]Edit: typo Quote Link to comment https://forums.phpfreaks.com/topic/31443-truely-global-operations/#findComment-145845 Share on other sites More sharing options...
obsidian Posted December 21, 2006 Share Posted December 21, 2006 @utexas_pjm - You're actually exactly right. I made another hasty post, and I think I've probably suggested something that is not best coding practice. Thanks for the wake up call. Yes, abstract classes should be intended to be inherited by a more specific class that can then be instantiated within your code. I simply chose to use abstract in this case because, by principle, you can still call the functions (although very generic) through the scope resolution operator and group them together in that way. Quote Link to comment https://forums.phpfreaks.com/topic/31443-truely-global-operations/#findComment-145866 Share on other sites More sharing options...
Eric_Ryk Posted December 21, 2006 Share Posted December 21, 2006 I also define classes abstract when I do not wish to have instances of them. I suppose you could just make the constructor private or something similar, but I've always found it easier to just make it abstract. Quote Link to comment https://forums.phpfreaks.com/topic/31443-truely-global-operations/#findComment-145920 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.