Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

2012-12-27

Turn OFF register_globals in PHP

Having now used two different hosting companies for PHP web site hosting, I would like to quick describe the configuration issue that have occured with both hosting companies. They both set PHP's register_globals configuration variable to On, which can have undesirable effects on your application and make you easily second guess why your application is behaving in a certain way.

When register_globals is set to On, this means that your variables are registered globally and available on every page. So if you declare a variable called $userCount and assign it some value in a page called script1.php and then have another variable with the same name ($userCount) but with a different meaning on another page (say script2.php), it will initially take the value assigned when script1.php was executed!

So let's say script1.php performed the following assignment:

$userCount = 5;

When script2.php executes, if it also has a variable called $userCount, its initial value will be 5! Very confusing!

So take my advice, if you can configure PHP yourself on your web hosting site, then set register_globals to Off, or ask your web hosting company to do this for you. 

Also note that the register_globals configuration variable has been removed as of PHP version 5.4.

2012-08-21

PHP Namespaces

I've been working on a PHP project lately and when I started working on it, I remember thinking to myself that there must be the equivalent to Java packages in this language. Otherwise loading/including/referencing classes from another class can be a bit of nightmare. There is the common PHP autoload pattern, but I find this is not enough and so I kept on digging and eventually ... success! Since PHP 5.3 there is the concept of PHP Namespaces, similar to Java packages which is very helpful in creating re-usable code. Basically in your class declaration, start it off with putting your namespace definition before anything else, like so:


namespace acme\utils;

class User {
...
}
?>

And then you can re-use your class from another class or PHP file, like so:


namespace acme\business;

use acme\utils\User;

class ProcessCommand {
...
$user = new User();
...
}
?>

Hope that helps and happy PHP coding!

Learn to build

There are a lot of technologies out there these days to work in: Flex, PHP, HTML, CSS, JavaScript, Ruby, the list goes on and on. And each one of them has their place in the grand scheme of things, each with its own strengths and weaknesses, each with its own particularities and each one has its appeal.

At some point in time, you will have to use one technology or another to get a job done, be it either by your own choice or because it is a requirement. And the technology of choice might not be one that you have used before. So what are you to do? Grab a book of course and starting reading. I've done that many times in the past. Another way is to read online tutorials and yes I've done this many times before as well. But honestly, the best way to learn is to ... build something! Ain't nothing like writing the lines of code yourself and seeing the results unfold right before your eyes. Really its the best way to learn, I can't stress that enough. It's the best way to learn the little hidden details of a language (or just a technology or framework for that matter) and to put what you have been reading into practice and really get a good grasp on things. You don't have to build anything complicated, just a simple contact manager will do, just enough so that you can really understand how something works.

So like my blog post title says, "Learn to build", or more like "build something" to "learn" :)

2011-06-24

Dreamweaver 5.5 is solid for HTML development

I recently upgraded to CS5.5 and I've been using Dreamweaver to build an HTML5/jQuery site with PHP on the server-side and I must say, it is one good product. It has everything you need a modern day development tool, from code-hinting (for HTML5 and PHP), to a design view (so you can see what you are building) and easy remote server synchronization.

Also, the CS5.5 release added much needed code-hinting for jQuery, one of the best JavaScript frameworks out there IMHO (I don't know where I'd be without it for JavaScript development). They have taken the jQuery support one step further and also added templates for jQuery Mobile, so you can use the framework to build mobile applications as fast as possible. There is further evidence of mobile development, in the way of support for CSS3 media queries and a magic "Multiscreen" button in the toolbar that allows you to preview what your page will look like on a phone, tablet or browser all in one go. Needless to say, Adobe has added some nice features to support current development trends.