Has anyone attempted to make PHP's system functions more Object-Oriented?
Asked Answered
M

4

10

I'm just curious if any project exists that attempts to group all (or most) of PHP's built-in functions into a more object-oriented class hierarchy. For example, grouping all the string functions into a single String class, etc.

I realize this won't actually solve any problems (unless the modifications took place at the PHP source code level), since all the built-in functions would still be accessible in the global namespace, but it would certainly make usability much easier.

Milton answered 15/9, 2008 at 1:56 Comment(0)
J
5

To Answer your question, Yes there exists several of libraries that do exactly what you are talking about. As far as which one you want to use is an entirely different question. PHPClasses and pear.org are good places to start looking for such libraries.

Update: As the others have suggested SPL is a good library and wraps many of built in php functions. However there still are lots of php functions that it does not wrap. Leaving us still without a silver bullet.

In using frameworks such as Cakephp and Zend (others too), I have noticed that they attempt to solve some of these problems by including their own libraries and building basics such as DB connectivity into the frame work. So frameworks may be another solution

Jabez answered 15/9, 2008 at 3:21 Comment(0)
I
7

Way too many times. As soon as someone discovers that PHP has OO features they want to wrap everything in classes.

The point to the OO stuff in PHP is so that you can architect your solutions in whichever way you want. But wrapping the existing functions in Objects doesn't yield much payoff.

That being said PHP's core is quite object oriented already. Take a look at SPL.

Infante answered 15/9, 2008 at 2:6 Comment(2)
SPL is definitely a step in the right direction. I just think it's extremely messy having so much core functionality in the global namespace. It seems ridiculous to me that I have to worry about naming collisions with what is built-in to the language.Milton
If you write your own code in an OO way, there's rarely any worry. Since the chances of conflicting with anything in the core is almost zero. And even if you do, it'll fail. I completely get where you're coming from though. PHP just doesn't have that flavor. Python might be a better choice.Infante
E
6

I think something like this is intergral for PHP to move forward. Being mainly a .Net programmer, I find PHP painful to work in with it's 1 million and 1 global functions. It's nice that PHP 5.3 has namespaces, but it doesn't help things much when their own libraries aren't even object oriented, let alone employ namespaces. I don't mind PHP as a language so much, but their API is terribly disorganized, and it probably needs a complete overhaul. Kind of like what VB went through when it became VB.Net.

Eurhythmics answered 15/9, 2008 at 2:4 Comment(2)
Agreed, but... their documentation is unparalleled.Infante
I definitely agree, I'd love to see this as a priority for PHP 6. Since they are now releasing namespace support in the forthcoming 5.3 release, I'm hoping the next logical step is to start moving their standard library functions to its own namespace/class heirarchy.Milton
J
5

To Answer your question, Yes there exists several of libraries that do exactly what you are talking about. As far as which one you want to use is an entirely different question. PHPClasses and pear.org are good places to start looking for such libraries.

Update: As the others have suggested SPL is a good library and wraps many of built in php functions. However there still are lots of php functions that it does not wrap. Leaving us still without a silver bullet.

In using frameworks such as Cakephp and Zend (others too), I have noticed that they attempt to solve some of these problems by including their own libraries and building basics such as DB connectivity into the frame work. So frameworks may be another solution

Jabez answered 15/9, 2008 at 3:21 Comment(0)
P
4

I don't agree. Object Oriented Programming is not inherently better than procedural programming. I believe that you should not use OO unless you need polymorphic behavior (inheritance, overriding methods, etc). Using objects as simple containers for code is not worth the overhead. This is particularly true of strings because their used so much (e.g. as array keys). Every application can usually benifit from some polymorphic features but usually at a high level. Would you ever want to extend a String class?

Also, a little history is necessary to understand PHP's odd function naming. PHP is grounded around The Standard C Library and POSIX standard and uses many of the same function names (strstr, getcwd, ldap_open, etc). This is actually a good thing because it minimizes the amount of language binding code, ensures that a full well thought out set of features (just about anything you can do in C you can do in PHP) and these system libraries are highly optimized (e.g. strchr is usually inlined which makes it about 10x faster).

Pelops answered 15/9, 2008 at 16:45 Comment(1)
Point well taken. Whether or not it is worth the overhead will also depend on the context though. We sacrifice performance for simplified code all the time - otherwise we'd all still be developing in assembly.Milton

© 2022 - 2024 — McMap. All rights reserved.