[ Content | View menu ]

5 ways to make php behave like asp.net

Written on November 23, 2006

A few observations of someone working with php but missing ASP.NET a bit (only a bit mind):

Use Smarty to separate content from code
The way .NET forces you to seperate your presentation into aspx pages is great - whilst PHP pages have a nasty tendency of these getting all mixed up (especially when you get a bit lazy). Smarty does a great job of preventing that, with the extra plus of letting you manipulate data in a similar way to xsl.
Make all pages post back on themselves
By automatically posting back on themselves .NET pages give you a much more familiar and encapsulated way of working with them (especially if you have come from a desktop programming background). By placing a form with a hidden input on each page you can recreate this, and distinguish between page loads and page posts.
One class per page
Using a class instead of a flat page give you much more control over what is going on and lets you share data between functions.
Inherit from a single page class with built in Smarty
.NET page come with loads of useful built in functions and properties. By adding a smarty object by default to a base class also you can preload it with things like page title, client scripts etc.
Use viewstate to retain state between postbacks
Viewstate in ASP.NET lets you maintain information between post-backs without resorting to dumping things in the session. You can create this by having an array in your base class that is encrypted at the point the page is loaded and stored in a hidden input. On post-back the input is plucked from the $_POST object and decrypted.

Filed in: code.

No Comments

Write comment - RSS Comments

Write comment