Monday 27 March 2017

Re: [dcphp-dev] Global Variables Question

Hi Whitney,

As a general rule, don't get in the habit of using global… ever. It blows through class and function walls and can create security holes. That being said, sometimes you have to maintain old code that still has it. If you run across it, make it your goal to refactor those out over time by passing variables and objects correctly through class and function properties.

As far as having at the head of a file vs inside a function or a class, don't use it at all ;)


On Mar 26, 2017, at 10:08 AM, Whitney Yiu <wyiu416@gmail.com> 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