 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Brandon Savage's Blog: Superglobals In Classes Revisited
by Chris Cornutt July 14, 2009 @ 07:51:11
Revisiting an earlier post dealing with superglobals and classes, Brandon Savage looks at an example of why its still a bad idea.
I asserted at the time that superglobals inside of a class violated some basic rules on what a class was supposed to do. Today, I am revisiting that discussion. The placement of superglobals inside a class creates an impossible situation for code reuse. [...] Ehat happens when we want to move this [code] to another site? Unless we leave our form fields named [the same] we'll have to modify the original code.
His alternative - a much better refactoring - lets the verifyCredentials method take in the username and password and has the calling script define where those come from, either from a local or global location.
voice your opinion now!
refactor revisit class superglobal
PHPBuilder.com: The ABC's of PHP Part 4 - How Variable Am I?
by Chris Cornutt April 02, 2009 @ 07:51:36
PHPBuilder.com has the next articles in their "ABCs of PHP" series posted today, a look at variables - what they are and how they're used.
To many beginners the subject of variables is usually pretty scary, and often a reasonably difficult concept to grasp, the reason for this however is usually because most modern languages require some kind of indication as to what type of data a variable will hold, this in turn often confuses beginners because they don't know what type of data relates to what kind of type.
They describe variables (using sample assignments like strings and numbers) and talk some about scope and how it affects their visibility. There's also a brief mention of the superglobals there close to the end.
voice your opinion now!
abc introduction series variable assignment superglobal
SitePoint PHP Blog: On $_GET and $_POST
by Chris Cornutt February 05, 2009 @ 11:14:33
On the SitePoint PHP Blog today Troels Knak-Nielsen takes a deeper look at two of the superglobals a lot of PHP developers take for granted - $_GET and $_POST.
When a PHP script is invoked by a web server, it is as the result of a HTTP request. A HTTP request has a target URI and that URI consists of different parts. One of these parts is the query. As the PHP process starts up, the query gets parsed into an associative array. And for some reason, somebody decided on the unfortunate $_GET, because it's what you use for GET requests - right? Wrong!
He points out that all HTTP requests, regardless if they're GET or POST will have that GET information (not necessarily in $_GET, though). He also mentions another commonly used (and sometimes abused) superglobal - $_FILES. His biggest gripe, though, is that the naming of the variables confuses the developer as to the true content of the HTTP request.
And I won't even comment on the nastiness of $_REQUEST.
voice your opinion now!
get files request superglobal http request content confuse
PHPBuilder.com: How to Upload Images Using PHP
by Chris Cornutt February 02, 2009 @ 10:23:00
The PHPBuilder.com site has a quick new tutorial showing how to upload images to your server via a PHP script (including some error checking).
One of the most frequently asked questions about PHP is "how can I use PHP to upload an image". In this article we'll discuss the details of how you can do just that!
Their script is done in three steps - an HTML form to accept the input, the PHP script to handle the upload (working with the $_FILES superglobal) and another HTML page to let the user know their upload was a success. When the file is uploaded, they check for a few things: the internal error PHP could throw, ensuring that the uploaded file exists and checking to ensure that the file is an image. Only then is it moved over to the true uploads directory to be stored.
voice your opinion now!
upload image example tutorial superglobal files
Brandon Savage's Blog: Keeping Superglobals Out Of Classes
by Chris Cornutt December 08, 2008 @ 07:57:24
In a new post to his blog, Brandon Savage makes a suggestion that could help in maintenance and debugging down the road - keep those superglobals out of your classes.
Let's ignore the security implications of the above code for just a moment, and focus on just the use of the superglobal. By using the $_POST superglobal array, we're effectively doing two things [in the example code]: relying on the field names and limiting code reuse.
He shows how to refactor the example into something a bit more reusable by changing the method call to pass in the given username and password instead of looking to the global for it. He does note, however, that there are some more correct uses for those superglobals:
There are some legitimate uses of superglobals in classes. One example is the use of the $_SESSION superglobal, which is often used for things like a user object. But I urge you to do so sparingly, when appropriate, rather than relying heavily on superglobals which are subject to change and may not give you the data you expect.
voice your opinion now!
class object oriented superglobal refactor reuse
David Otton's Blog: Neat PHP Tricks How To Assign References to Globals
by Chris Cornutt November 10, 2008 @ 09:32:18
David Otton has a new neat PHP trick posted today - assigning references to global values.
What follows isn't so much a PHP trick as a fix for something that really should work, but doesn't. Although the manual implies that the behaviour described below is specific to Zend Engine 1, all my tests were performed against Zend Engine 2.2, PHP 5.2.5.
His example compares making a new stdClass both with and without a reference on the it and var_dumps out the result. The method with the reference fails silently, however and isn't able to correctly assign it to the global. He recommends a work-around though - setting it directly to the $GLOBALS superglobal.
voice your opinion now!
reference global superglobal trick assign object example
Sebastian Bergmann's Blog: Global Variables and PHPUnit
by Chris Cornutt June 17, 2008 @ 08:49:19
Sebastian Bergmann has a new post today about a feature of the PHPUnit unit testing tool that has the possibility of breaking when objects are introduced - backing up the globals.
It is hard to test code that uses singletons. The same is true for code that uses global variables. Typically, the code you want to test is coupled strongly with a global variable and you cannot control its creation. An additional problem is the fact that one test's change to a global variable might break another test.
You can disable the backup option if you'd like by setting the $backupGlobals option in your test to false. This lets PHPUnit know that you want to leave the globals (and superglobals) alone during the run.
voice your opinion now!
global variable phpunit unittest backup global superglobal test disable
|
Community Events
Don't see your event here? Let us know!
|