Xu Wei Jie Posted March 15, 2009 Share Posted March 15, 2009 Hi all, How do I include once a library? I mean not in the fashion of using include_once(). But implementing something that is similar to it If the library is ever loaded, I would escape from loading it again. Will this code work if I put in the beginning of the library code? library.php <?php if (isset($pp_loaded__)) return 1; $pp_loaded__ = true; //load library functions here ?> Quote Link to comment https://forums.phpfreaks.com/topic/149513-including-once/ Share on other sites More sharing options...
Daniel0 Posted March 15, 2009 Share Posted March 15, 2009 Just store a list of included libraries and check against it each time you are going to include something. If it already exists in the list then abort. Quote Link to comment https://forums.phpfreaks.com/topic/149513-including-once/#findComment-785191 Share on other sites More sharing options...
Xu Wei Jie Posted March 15, 2009 Author Share Posted March 15, 2009 Actually i want to achieve something like when the user includes twice or more times the library itself, PHP won't prompt warnings like re declared functions and etc. //test.php <?php include("library.php") include("library.php") include("library.php") include("library.php") ?> Using a list to keep track may be a good idea but if multiple files are involved, the list may be hard to pass from one file to another. It gets complicated. Consider this example //a.php <?php include("library.php"); include("b.php"); ?> //b.php <?php include("library.php"); ?> Thus I want to find a way to return out of the included library if it was ever loaded before. Quote Link to comment https://forums.phpfreaks.com/topic/149513-including-once/#findComment-785196 Share on other sites More sharing options...
Daniel0 Posted March 15, 2009 Share Posted March 15, 2009 That's what you use include_once for... Quote Link to comment https://forums.phpfreaks.com/topic/149513-including-once/#findComment-785198 Share on other sites More sharing options...
Xu Wei Jie Posted March 15, 2009 Author Share Posted March 15, 2009 I want to simulate the same behavior without using include_once. I wish to design the library such that the programmer need not be mindful whether to use include or include_once. He/She just need to include the library using whichever include he/she prefer and the library will detect for itself whether it has been loaded or not loaded before. Quote Link to comment https://forums.phpfreaks.com/topic/149513-including-once/#findComment-785204 Share on other sites More sharing options...
Daniel0 Posted March 15, 2009 Share Posted March 15, 2009 Whatever you come up with will most definitely be less efficient than using the built-in include_once()/require_once() functionality because those are written in C. Just always use _once and you won't have to think about it. Quote Link to comment https://forums.phpfreaks.com/topic/149513-including-once/#findComment-785208 Share on other sites More sharing options...
Mark Baker Posted March 15, 2009 Share Posted March 15, 2009 If you really have to rewrite PHP, why not take a look at the APD extension override_function('include', '$fileName', 'include_once($fileName)' ); Quote Link to comment https://forums.phpfreaks.com/topic/149513-including-once/#findComment-785375 Share on other sites More sharing options...
Xu Wei Jie Posted March 16, 2009 Author Share Posted March 16, 2009 Anyway, I tried if(isset($loaded)) return else $loaded="set"; This code does not work though because $loaded seems to be a new instance every time the library is included. Another way is to add another level of include_once inside the library =) library.php <?php include_once("library_functions.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149513-including-once/#findComment-785572 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.