Jump to content

Objects & Functions


shane18

Recommended Posts

how can i globalize that object in the function so i don't gota link back to it every time like this blah($DB)

 

You learn to organize your stuff properly so you don't have to use things like that ;)

 

Seriously, here are things I would do to PHP 6 if it was my decision:

1) Remove the global keyword and remove all the super globals.

2) Stop letting people do stupid stuff like using undefined variables and accessing non-existent array indices, i.e. bump it from a notice to a fatal error.

Link to comment
Share on other sites

how can i globalize that object in the function so i don't gota link back to it every time like this blah($DB)

 

You learn to organize your stuff properly so you don't have to use things like that ;)

 

Seriously, here are things I would do to PHP 6 if it was my decision:

1) Remove the global keyword and remove all the super globals.

2) Stop letting people do stupid stuff like using undefined variables and accessing non-existent array indices, i.e. bump it from a notice to a fatal error.

 

Introduce strong typing. Require all variables to be declared at the beginning of the function's body....

 

In no time we'll have Pascal Hypertext Preprocessor :D

Link to comment
Share on other sites

I have a function that needs to use the mysqli object i created at the top of the page... and im trying to figure out the best way to do this...

 

As has been stated, the best way to do this is to pass the object into the function as an argument. Topic resolved, move on with your day.

Link to comment
Share on other sites

From Wikipedia:

They are usually considered bad practice precisely because of their nonlocality: a global variable can potentially be modified from anywhere, (unless they reside in protected memory) and any part of the program may depend on it. A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity. See Action at a distance. However, in a few cases, global variables can be suitable for use. For example, they can be used to avoid having to pass frequently-used variables continuously throughout several functions.

 

The use of global variables makes software harder to read and understand. Since any code anywhere in the program can change the value of the variable at any time, understanding the use of the variable may entail understanding a large portion of the program. They make separating code into reusable libraries more difficult because many systems (such as DLLs) don't directly support viewing global variables in other modules. They can lead to problems of naming because a global variable makes a name dangerous to use for any other local or object scope variable. A local variable of the same name can shield the global variable from access, again leading to harder to understand code. The setting of a global variable can create side effects that are hard to understand and predict. The use of globals make it more difficult to isolate units of code for purposes of unit testing, thus they can directly contribute to lowering the quality of the code.

 

As to your problem. I just tried running this code:

$mysqli = new mysqli('localhost','root','','test');

function blah() {

  global $mysqli;
  
  var_dump($mysqli);

}

blah();

 

And it runs fone, so there must be something else wrong (like another global variable called $DB)

Link to comment
Share on other sites

How do they cause problems? I just wana beable to global $DB and because its an object an error is being created; in a function so that i don't gota pass it as a parameter...

 

Because it tightly couples things and makes it much easier to screw up and maintain in the long run. That's why I'd rather see it removed from PHP entirely; it's not obvious to the beginner why it's a bad idea.

Link to comment
Share on other sites

$_SESSION, $_POST globals aren't bad right? aren't they required?

 

No, actually they're not required for reading data (though it doesn't support the FILTER_SESSION). See filter_input. As for writing, that shouldn't happen to $_POST (though I see people doing it regardless) and having to write to $_SESSION is a design flaw, though it can be abstracted away using something like Zend_Session or something you've written yourself.

 

Even if you can read POST data using filter_input(), you should still actually pass the data as a parameter to the consumers to ensure portability and decoupling.

Link to comment
Share on other sites

$_POST and $_GET (and other superglobals) are a bit different, since they're part of language itself. Still, you should be rather careful when toying with them.

I had some funny experiences once, when I was rewriting some of my functions that relied on $SERVER variable, to be able to use them in CLI (where there is no $SERVER variable).

 

Since then, I only use them in global scope, and if I need their values in functions, I pass them through parameters.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.