PHP Manipulating Strings, OO Style
Whenever using php to output data on the web, it is common place to have to format
it so that it is visually stimulating, or to add emphasis. Depending on your configuration,
this may require echoing large amounts of html each time you output data. You may be using a template
engine, or a framework that allows view scripts, where the file is primarily html, but you put in place-holders
or echo the data wherever needed, this arguably is the easiest to debug, fastest to change, and the best
Sometimes, however, you may not have the ability to use templating engines, or a framework, and may have to rely on
your own functions for manipulating data for output. Having to echo html several times over and over again, to
change simple things like color, or creating elements is a pain, and not friendly. Say you have an error message style, or a header
style, and everytime you use this data, you have to again and again echo html elements. This can lead to several
mistakes in html, and or php code.
What you should always remember when thinking of your code, is DRY, or Don't Repeat Yourself. Whenever
you find you are coding the same thing multiple times, it is time to step aside, think, and refactor.
In Comes OOP. Using the power of OOP in PHP, PHP5 especially, we can create objects that will handle string manipulation.
Creating the Base Class
So now that we know what we want to do, manipulate strings for output, lets create a class that we will expand on
to handle these functions.
NOTE: Remember, longer names do not take longer to execute in php, so make sure you name your methods, objects, and properties
something that is self-explanatory on function and return.