Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
Am I the only developer out there who has never used any form of global in any php application? is there any need for them?
what are your opinions and could you give me an example of why and indeed where I should/could be using them.
I know this is rather open ended, but I do have some strong opinions on coding practises and this is one area I'd like to hear opinions on before coming back with my own thoughts
Globals are not the devil, you know. In inexperienced hands, they can cause problems, but otherwise they speed up both the production process and the execution.
There is a difference between declaring variables in the global scope and accessing variables in the global scope from within functions and classes. It is the latter that is considered a poor practice because the possibility for multiple dependencies which can lead to unexpected side effects and difficult to maintain code.
I think most PHP programs declare some variables in the global scope. Limiting the number of these may be a good practice, but trying to eliminate any variables in the global scope may lead to design workarounds.
Yep that's pretty much what mine looks like. A couple of requires first. I voted that I never use them but a couple of days ago I used about 8 or so in a CLI script. But the whole thing was simple enough for that not to be a problem. I didn't declare any classes either for instance. As with anything generally deemed hacky, don't use them, unless you are putting together something small and quick when they'll usually speed things up.
Perhaps you could explain why doing that as opposed to doing what is in Application's constructor in the global scope is a better practice? And I assume that you also would need to include the class or an autoload function:
I'm sorry, but that's a stupid answer - simply because there is no good answer. Doing that as opposed to doing what is in Application's constructor cannot be justified, it's pretty much like taking everything in the index.php file and throw it into some other file and then include this other file from index.php - this will take the same time as without doing it at best, and will probably be slower at worst.
Oren wrote:I'm sorry, but that's a stupid answer - simply because there is no good answer.
Please don't lower the level of the discussion with arguments like this.
Doing that as opposed to doing what is in Application's constructor cannot be justified, it's pretty much like taking everything in the index.php file and throw it into some other file and then include this other file from index.php - this will take the same time as without doing it at best, and will probably be slower at worst.
I'm not sure what you're trying to say here. Can you provide an example of your approach?