Re: [PHP] Classes and access to outside variables



On 9/30/07, 潘志彬 <ryu.pan@xxxxxxxxx> wrote:

$dbh = 'test';

class search_helper extends AjaxACApplication
{
/**
* Database connection details
*/

// announce global variable before use it
global $dbh;

$db_hostname = $dbh;



global variables inside a class ??
as Martin said this is a bad practice.
if you were going to use a global variable inside a class, why bother w/
a class in the first place?
the point is to encapsulate, ie. hide implementation details from the user.
by relying on a global variable, the class is open to behavior modification
by
external code that does not have to go through its well-defined public
interface
to do so. thats why its a bad practice.

-nathan


Relevant Pages

  • Re: Global Variables in OOP and Python
    ... > Agree about from module import * being bad, but it is still generally poor ... > practice for the same reason using global variables is generally poor ... Namespace pollution or name clashes are another issue and we have to ...
    (comp.lang.python)
  • RE: Questions regarding hidding vs closing forms
    ... I would not say it's a bad practice, especially if that Form is eatting up a ... > practice to close the current form when openning a new form? ... > lot performance and memory issues? ... > global variables for these forms, so that I could Show the hidden form from ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Global Variables on a VB.NET Web Page
    ... I have found that I can create global variables easily on a web page by ... placing the dim statement before the first "private sub" in a program: ... Public Sub Page_Load ... memory if the user clicks away - or is this a "great practice". ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Global Variables in OOP and Python
    ... >> Agree about from module import * being bad, but it is still generally poor ... >> practice for the same reason using global variables is generally poor ...
    (comp.lang.python)
  • ASP.NET Page Variables Persistence
    ... I am designing an ASP.NET application in which particular pages require global variables that need to be persisted during postbacks. ... What is the best practice to do this? ...
    (microsoft.public.dotnet.framework.aspnet)