NotionCommotion Posted July 3, 2016 Share Posted July 3, 2016 How can I add a function to my main PHP file, yet not let it be available withing a class? For instance, prevent baseFunction() from being available within foo. <?php class foo{ public function bar() { echo(baseFunction()); //Global echo($baseVariable); //Not global } } function baseFunction() {return 'baseFunction';} $baseVariable='baseVariable'; $foo=new foo(); $foo->bar(); Quote Link to comment https://forums.phpfreaks.com/topic/301424-preventing-main-file-functions-from-being-global/ Share on other sites More sharing options...
Jacques1 Posted July 3, 2016 Share Posted July 3, 2016 (edited) What exactly are you trying to achieve? The only way to get a truly local function is to use a closure (anonymous function): <?php class Foo { public function bar() { // cannot be called here $baseFunction(); } } // closure $baseFunction = function () { echo 'Closure'; }; // can be called here $baseFunction(); $foo = new Foo(); $foo->bar(); Or you could use namespaces. This doesn't actually hide the function, but it makes it harder to access it. Edited July 3, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301424-preventing-main-file-functions-from-being-global/#findComment-1534201 Share on other sites More sharing options...
NotionCommotion Posted July 3, 2016 Author Share Posted July 3, 2016 What exactly are you trying to achieve? I always have a single file index.php which does a little work, creates an object from a class, and does something. Sometimes a function in that file will make the code more concise, however, felt that it shouldn't be global. Quote Link to comment https://forums.phpfreaks.com/topic/301424-preventing-main-file-functions-from-being-global/#findComment-1534202 Share on other sites More sharing options...
Jacques1 Posted July 3, 2016 Share Posted July 3, 2016 If the project is mostly object-oriented, consider moving the logic from the main script into a separate class. This will immediately solve the scoping issues and is generally cleaner. Or use closures as explained above. But I feel this is more like a hack. Quote Link to comment https://forums.phpfreaks.com/topic/301424-preventing-main-file-functions-from-being-global/#findComment-1534203 Share on other sites More sharing options...
NotionCommotion Posted July 3, 2016 Author Share Posted July 3, 2016 Thanks Jacques1, Will go for the separate class approach, but will consider the closure solution when it makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/301424-preventing-main-file-functions-from-being-global/#findComment-1534204 Share on other sites More sharing options...
requinix Posted July 4, 2016 Share Posted July 4, 2016 I'd avoid the closures: they come with increased memory usage that does not get cleaned up until you use hundreds or thousands of closures. By design, which I still don't understand but is intentional. If you want non-global functions, use namespaces or utility classes. Quote Link to comment https://forums.phpfreaks.com/topic/301424-preventing-main-file-functions-from-being-global/#findComment-1534205 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.