Writing Solid PHP Code 3 - Avoid the Spaghetti
January 28, 2009
Unfortunately, a lot of PHP code written today is procedural spagetti code. For smaller applications it may be okay, but larger applications we need to make use of classes.
Classes allow you to make new object variables that can perform all the functionality of the class. Take a look at an example of a class:
class car { // Define the function start() with no parameters function start() { $started = true; return $started; } }
We will create a new car object with the following code:
$myCar = new car; $isStarted = $myCar->start();
Using classes, we can focus on the specific roles of each piece of code and test it accordingly. If we have classes we know have been tested to work, we can use inheritance to extend the functionality of these classes. We are building a solid base and building up from there.
All of your scripts core functionality should use classes. Classes make code more reusable, easier to test, and easier to read and maintain.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.











