tags:

views:

151

answers:

4

I recently had an idea to create my own String class to make using PHP's functions easier. Instead of strlen($str) I write $str->length(). Makes it easier to remember parameter orders in certain functions, like substr.

I ran some timing scripts on it and found that it's about 5 times slower than using the regular functions. I haven't tested this in a real app yet, so I don't know how negligible that will be (1ms vs 5ms or 100ms vs 500ms?).

Anyway it struck me that now PHP is now focusing more on OOP, wouldn't it make sense for strings, arrays and other basic types to be object oriented? They could then name the functions better and code would just "feel" nicer. And slowly phase out the old way of doing things. Any pros/cons to this?

+4  A: 

You could always document your code and just use the functions that PHP has provided. It is hard to tell whether or not it is going to affect you in the long run. There are many different factors that can influence that. Give it a shot, maybe and if it does not work out switch back to the original way.

Personally, I would just keep my document well documented instead of getting too fancy. If you want to make the push into more OOP your best bet is to look into a Framework.

I wish PHP would make it that simple from the start. The code would look so neat.

Chris B.
And if using a framework still doesn't do it for you, it might be time to look into a different language, particularly one that is truly object-oriented.
Jeff L
+1  A: 

I think it would be nice, but it wouldn't be PHP any more...

Greg
A: 

I understand why you're doing this, but libraries and abstractions that do nothing else besides "make it easier on the programmer" are a waste of time, in my humble opinion. They're rarely efficient, they're fluff, and they're even kind of pretentious.

PHP has a lot of faults, that is to be certain, but you'll spend a lot of time that could be better spent elsewhere if you try to invent workarounds for them everywhere you go.

Peter Bailey
+1  A: 

Sure, $somestring->length() is nice but, on the flipside, you have to $somestring = new String('asdf...') whenever you make a string and then constantly convert Strings to strings and vice versa. You probably end up making things harder to write & maintain in the long run.

I don't really see the PHP language ever changing in this way - it would change too much of the fundamental language. If you want a language that is like this, you're going to have to change languages, rather than hope for the language to change.

Sean McSomething