Future Proofing Our Code
Due to all the changes that PHP has going through over the last few years, I've spent a lot of time rethinking my coding practices. Functions such as ereg_replace() have been depreciated and eventually we won't be able to use functions like mysql_query(). With these types of changes, who knows what's safe. Let's look at what can be done to future proof our code.
When writing PHP scripts, there's one thing which appears in most if not all of them. To display something to screen, we use print or echo. These options are essentially the same thing. What happens when the decision is made to remove one of them? Replacing hundreds, thousands, or more of these statements throughout our website(s) isn't going to be fun.
Instead of waiting for the inevitable change, a solution can be developed to limit the effects. Let's create a PHP class to help out.
<?php
class myPrint {
public function displayThis($thingToDisplay) {
print $thingToDisplay;
}
}
?>
The "myPrint" class just needs to be imported as needed and any reference to print or echo should be replaced with code like the following:
<?php
//CREATE AN INSTANCE OF THE CLASS
$myPrint = new myPrint();
//USE THE CLASS FOR THE FIRST TIME
$myPrint->displayThis('hello world');
?>
Conclusion
Now, if the print statement is removed in favor of echo, we only need to replace the single reference. With all those pesky print / echo statements out of the way, this 15-part series can continue next week by repeating the process with all those if/else statements. Next time around, however, we'll be using the all-important global keyword.
0 Comments
There are currently no comments.
Leave a Comment