Vermillion Posted March 9, 2010 Share Posted March 9, 2010 Hi guys, it has been a while since the last time I came here... I am now working on my website in the most "professional" way for me. I am using OOP, Namespaces, and all that, and here is where I have a question: The root namespace is called "FreedomAPI", and in that namespace I will put all the functions that are too general to fit in another subnamespace. I have no problem with that, the thing is that I don't know where to create those general functions. would it be a good solution to put them in a "functions.php", and then just include that file with the rest of my classes? My framework is sorta complex, and I have had a horrible experience trying to include all the files in different levels of directories in the servers, so I don't know what to do about where to write those functions. Quote Link to comment https://forums.phpfreaks.com/topic/194695-should-i-create-a-general-functionsphp-file/ Share on other sites More sharing options...
nafetski Posted March 9, 2010 Share Posted March 9, 2010 If you are going to code your website in a professional manner, (and if your'e already using OOP principles) then you would be much better off by doing the following... #1 - Set an __autoload() function to load classes automatically #2 - For your "general functions" namespace them into a class, and call them statically. Example Class user_management(){ public static function authorize_user() { // Code here. } public static function remove_user() { // Code here. } } What this will allow you to do is keep your grouped functions in a common namespace (inside of your class) and also only load the functions if that class is required (lowering your bloat). To call any of those functions you woudl simply go $var = user_management::remove_user(); Voila! Best practice, and will help you keep sane. If you haven't looked into autoloading, or have any particular questions please let me know. (Keep in mind, this isn't the ONLY way to do this...but I definitely think it's the best -Nafetski Quote Link to comment https://forums.phpfreaks.com/topic/194695-should-i-create-a-general-functionsphp-file/#findComment-1023883 Share on other sites More sharing options...
Vermillion Posted March 9, 2010 Author Share Posted March 9, 2010 Hey there! First of all, thanks a bunch for the reply. I really appreciate it. I have been into PHP for 4 years and into OOP for 3 years and I never heard about __autoload() haha. Can you give me more info about it? I will really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/194695-should-i-create-a-general-functionsphp-file/#findComment-1023889 Share on other sites More sharing options...
nafetski Posted March 9, 2010 Share Posted March 9, 2010 Sure thing It's really one of those amazing tidbits that a lot of people just don't know exists. PHP added something called "Magic Functions (haha, wish I was kidding)" sometime after PHP5. Here is a link.. http://www.php.net/manual/en/language.oop5.magic.php What happens is everytime a class is called (or an object is initialized) that the class definition HASN'T been included yet, this php function gets called. If you write all of your helper functions inside of classes, and if you keep all of your classes in say...a maintainable directory structure, you could do something as simple as this. function __autoload($class) { require_once 'classes/'.$class . '.php'; } What that does is say "Ok, this class wasn't included...but I'm going to try and include it at runtime in the classes/classname.php". This would be the most BASIC implementation of autoload, but if you follow a certain naming convention you could do some really fun stuff with it. function __autoload($class) { $class_name = explode("_",$class); switch($class_name[1]) { case 'controller': require_once 'classes/controllers/'.$class . '.php'; break; case 'model': require_once 'classes/model/'.$class . '.php'; break; } } That way you could go something like $var = new User_Controller(); And it would know to look in the controller directory to include that class file. For what it's worth, all of this logic and implementation I've pretty much just stolen from how Kohana (framework, if you haven't looked at it I think you might love it handles classes and autoloading. Anyway, let me know if that helps! Quote Link to comment https://forums.phpfreaks.com/topic/194695-should-i-create-a-general-functionsphp-file/#findComment-1023894 Share on other sites More sharing options...
Anti-Moronic Posted March 9, 2010 Share Posted March 9, 2010 Second everything above. __autoload is special But, try to also discern between when you should use static classes and when you should use objects. Static classes should be used for common functions which should be readily available anywhere within your code. Things like validation functions, access functions, parsing functions and common output functions. Objects are better used for storing, retrieving, and managing complex, reusable and extend-able code, and are especially magic when you interface them with each other. I'm sure somebody else can give you better guidelines on when to use one over the other, but that's the general gist. Quote Link to comment https://forums.phpfreaks.com/topic/194695-should-i-create-a-general-functionsphp-file/#findComment-1023910 Share on other sites More sharing options...
nafetski Posted March 9, 2010 Share Posted March 9, 2010 Well said The main reason I use public static functions is only if it's a factory method (to create an object) or helper functions. Quote Link to comment https://forums.phpfreaks.com/topic/194695-should-i-create-a-general-functionsphp-file/#findComment-1023917 Share on other sites More sharing options...
Vermillion Posted March 9, 2010 Author Share Posted March 9, 2010 Alright, I get it for the most part. But where exactly do I put that __autoload() function? Also, here is a hierarchy of my work so far, I hope this can help you help me (file names are in blue, normal folders in black): www - projectfreedom -- FreedomAPI.php -- FreedomAPI --- class.DateTime.inc.php --- Connection ---- Communication ----- class.Message.inc.php ----- class.PrivateMessage.inc.php I test everything in the FreedomAPI.php file. Eventually, I will make more directories inside www and inside projectfreedom, so the __autoload function will become very handy, once I know how to use it lol. Quote Link to comment https://forums.phpfreaks.com/topic/194695-should-i-create-a-general-functionsphp-file/#findComment-1023920 Share on other sites More sharing options...
nafetski Posted March 9, 2010 Share Posted March 9, 2010 Well, you will want to have a bootstrap file (that always gets called) and you can put your autoload function in there. If you don't already bootstrap a function, you can actually set up php to automatically include a file before every php script in your php.ini settings I'm also pretty sure there is a way to set the autoload function in php.ini as well...but like I said, I'm a Kohana developer so I just use what's in the framework. Shouldn't be hard to replicate tho! Quote Link to comment https://forums.phpfreaks.com/topic/194695-should-i-create-a-general-functionsphp-file/#findComment-1023923 Share on other sites More sharing options...
Vermillion Posted March 10, 2010 Author Share Posted March 10, 2010 Is there a way to do it without modifying the php.ini file? My host will not be dedicated unfortunately . Quote Link to comment https://forums.phpfreaks.com/topic/194695-should-i-create-a-general-functionsphp-file/#findComment-1024010 Share on other sites More sharing options...
nafetski Posted March 10, 2010 Share Posted March 10, 2010 Most settings that you can set in php.ini can be set in .htaccess Quote Link to comment https://forums.phpfreaks.com/topic/194695-should-i-create-a-general-functionsphp-file/#findComment-1024015 Share on other sites More sharing options...
Vermillion Posted March 10, 2010 Author Share Posted March 10, 2010 Alright, I swear I am close on solving my whole problems for once and for all. Please bear with me till then. I have never been good with htaccess. I am trying to find a way to use the auto_prepend_file with it with no luck. Also, is there a way to include all the files from the API with that statement at once, or can I just include one? It would be very nice to be able to include all my API in just one htaccess so I can use it without creating a require hell once again. Quote Link to comment https://forums.phpfreaks.com/topic/194695-should-i-create-a-general-functionsphp-file/#findComment-1024028 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.