Tuesday, 28 March 2017

[dcphp-dev] Re: Global Variables Question

I agree with Ray that dependency injection is a more elegant solution to the problem. It makes certain resources, for example, a database connection, available throughout an application or framework, while minimizing mutability and the problems & confusion that come with it. Here's another article that talks about it in the context of the broader concept of Inversion of Control: https://martinfowler.com/articles/injection.html.

For a more concrete example of global variables at work, check out the source code for WordPress or common WordPress plugins, where you'll see a smattering of "global $post" (the current blog post) and "global $user" (the current user). Then consider this question: should the record of the current user really be open to global changes? If it's altered within a certain plugin (i.e., module), changing the user's role from guest to administrator, what mayhem might ensue elsewhere in the system?

I would also add--to give a bigger picture view--that functional programming is taking off because of this very problem, giving rise to (relatively) new languages such as Scala, Clojure, etc. The functional programming paradigm dictates that programs be stateless and immutable (the very antithesis of global variables and state), which can have huge benefits for certain applications (and be highly impractical for others).

On Tuesday, March 28, 2017 at 8:44:11 AM UTC-4, Ray Paseur wrote:
Hi, Whitney.  Here's my take on the issue.
https://www.experts-exchange.com/articles/19999/PHP-Design-Avoiding-Globals-with-Dependency-Injection.html

On Monday, March 27, 2017 at 8:24:57 AM UTC-4, Whitney Yiu wrote:
Hi all,

I am reading a book (PHP and MySQL Web Development - Thomson, Welling; 5th Ed.; Addison-Wesley) and am trying to understand the following passage regarding global variables:

You can also use the global keyword at the top of a script when a variable is first used to declare that it should be in scope throughout the script. This is possibly a more common use of the global keyword.

Can someone explain a use case for using a global variable at the top of a script? I thought that if a variable is declared outside of a function, then it is accessible throughout the script (except inside functions). I think I am missing something here.

Thanks,
Whitney 

--
You received this message because you are subscribed to the Google
Group: "Washington, DC PHP Developers Group" - http://www.dcphp.net
To post, send email to washington-dcphp-group@googlegroups.com
To unsubscribe, send email to washington-dcphp-group+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/washington-dcphp-group?hl=en
---
You received this message because you are subscribed to the Google Groups "Washington, DC PHP Developers Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to washington-dcphp-group+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

0 comments:

Post a Comment