PHP String Object

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
francoselem
Forum Newbie
Posts: 2
Joined: Sun Feb 23, 2014 6:41 pm

PHP String Object

Post by francoselem »

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.

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 );
	}

}
An object of this type would output the following

Code: Select all

object(String)[1]
	public 'raw' => string 'Hello, World' (length=12)
	public 'htmlEntities' => string 'Hello, World' (length=12)
	public 'length' => int 12
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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP String Object

Post by VladSun »

I'd rather see the String interface (no implementation) first.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP String Object

Post by requinix »

If you're talking about an official implementation, it's been talked about but puts people on edge as it starts changing the nature of the language itself. A related concept is autoboxing.

Unofficially? There are a lot of string functions to cover - make sure you cover all of them somehow, otherwise the inconsistency would drive people crazy. But a PHP solution means every string function call gets an additional function call, and the performance could be noticeable. IMO the best solution would have to be an extension, and one that's kept up to date whenever new string functions are added or old ones removed.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP String Object

Post by Weirdan »

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.
You don't have to spend hours writing str_word_count() and substr_count(). They've been there for years.
Post Reply