background preloader

PHP

Facebook Twitter

PHP Advent 2011 / Cracks in the Foundation - Vimperator. PHP has been around for a long time, and it’s starting to show its age.

PHP Advent 2011 / Cracks in the Foundation - Vimperator

From top to bottom, the language has creaky joints. I’ve decided to take a look at how things got to this point, and what can be (and is being) done about it. I start out pretty gloomy, but bear with me; I promise it gets better. In the Beginning, There Was Apache and CGI And there was much rejoicing. In 1994, Rasmus Lerdorf created the “Personal Home Page Tools,” a set of CGI binaries written in C. PHP is a language of convenience. Unfortunately, nothing’s perfect — PHP included. Backward Compatibility Is a Female Dog Backward Compatibility (or BC) is the bane of every library and app writer in existence. When BC is broken, apps break. Sometimes, the source of the problem is correctly identified, and the PHP developers are berated for trying to make a better language. PHP 4.4 was released to fix a bug that caused memory corruption when references were misued. The results of all this are twofold. I Can See Clearly Now. JavaScript-style object literals in PHP / Stoyan's phpied.com - Vimperator.

The object literal notation in JavaScript looks like: or var fido = {}; fido.name = "Fido"; fido.barks = true; From assoc arrays to objects In PHP you would call that an associative array.

JavaScript-style object literals in PHP / Stoyan's phpied.com - Vimperator

$fido = array( 'name' => "Fido", 'barks' => true); And you can easily make it an object too: $fido = (object)$fido; echo gettype($fido); Or if you want to start with a blank object and add stuff to it: and then $fido->name = "Fido"; $fido->barks = true; A little explanation maybe: objects in JavaScript are hashes, maps, whatever you decide to call them.

Objects in PHP need a class, but the new stdClass() lets you start quickly without the class {...} jazz. So far - so good. Methods anyone? JavaScript doesn't care about properties versus methods. Fido.say = function () { if (this.barks) { return "Woof! " Turns out, since PHP 5.3 there are closures in PHP too. $fido->say = function() { if ($this->barks) { return "Woof"; }}; The difference is that $fido->say() won't work.

Say is not a method. And a sprinkle of magic. A Better Login System. Net.tuts+ has published several great tutorials on user login systems.

A Better Login System

Most tutorials only deal with authenticating the user, which allows for two levels of security: logged in and not logged in. For many sites, a finer degree of control is needed to control where users can go and what they can do. Creating an access control list (ACL) system will give you the flexibility for granular permissions. Introduction Imagine you are running a great tutorial site that lets users learn about a wide variety of web development techniques. Your problem You want to restrict users' to only specific pages that their particular account allows access to. The solution Implementing an access control list will allow you a great deal of control over what users can and cannot access on your site.

If you view the demo, available with the downloadable source code, you will be greeted with an index page that tests the ACL for each user. Step 1: Create the Database Step 2: Database Include Step 3: Create the ACL Class.