PHP String Object
Posted: Sun Feb 23, 2014 6:54 pm
Hello!
Do any of you know why we have not seen a more Object Oriented approach in PHP when it comes to Strings. It seems so reasonable to adopt a mechanism much like JavaScript's where every string is an object. For instance, I wrote a small little snippet that would highlight a few benefits when it comes to strings.
An object of this type would output the following
This way we wouldn't have to spend hours writing user defined functions like the amount of words in a string, or amount of occurrences of sub-strings that appear in a string object. Keep in mind this is a very quick example that I jotted down while writing the Snippet Master WordPress plugin, so my apologies if my point hasn't rendered distinguishable by that short snippet. Do any of you think something like this would be useful and worth developing further.
Please note that I have not done research on any possible PHP "String Objects" out there, I just sort of wanted to spectate feedback from this community.
Anyway, I appreciate any responses.
Do any of you know why we have not seen a more Object Oriented approach in PHP when it comes to Strings. It seems so reasonable to adopt a mechanism much like JavaScript's where every string is an object. For instance, I wrote a small little snippet that would highlight a few benefits when it comes to strings.
Code: Select all
class String {
public $raw;
public $htmlEntities;
public $length;
function __construct( $string ) {
$this->raw = $string;
$this->htmlEntities = htmlentities( $this->raw );
$this->length = strlen( $this->raw );
}
}
Code: Select all
object(String)[1]
public 'raw' => string 'Hello, World' (length=12)
public 'htmlEntities' => string 'Hello, World' (length=12)
public 'length' => int 12
Please note that I have not done research on any possible PHP "String Objects" out there, I just sort of wanted to spectate feedback from this community.
Anyway, I appreciate any responses.