4

I have been writing in PHP for just over half a year now, and whilst I am a long way off being an expert, I can get around quite easily and turn out scripts for whatever I need. I come from an object-oriented background and this is something that PHP seems to use very little of, if at all, in its default libraries.


Most external libraries I use or create, work with an object-oriented design whereas the defaults seem to use the next example. I'll use the file/writing reading process as an example:

$file_path = "/path/to/file.txt";
$file_handle = fopen($file_path, "w+");
$content = fread($file_handle, filesize($file_path));
fclose($file_handle);

Now to me, it would make more sense to be using a design that looks something like this:

$file_handle = new FileStream("/path/to/file.txt");
$content = $file_handle->read();
$file_handle->close();

Now I am quite sure that there will be a definite reasoning behind this, as the same idea is applied to strings, arrays, cURLs, MySQL queries etc. I would be interested to know what it is.

So, if it is better to write distinct functions taking a handle or resource as a first parameter e.g.

object_method($handle, $value);

Then why do most popular (external) PHP libraries prefer to use:

$object->method($value);

And which should I be using when writing my own libraries and applications?

3
  • 1
    Because OOP in PHP is relatively new, and only became useful in the later 5.x versions. PHP has a LOT of old baggage to drag around. Commented Feb 23, 2012 at 19:20
  • Can't speak for the authors, but my guess is legacy reasons; if it works, don't break it. Commented Feb 23, 2012 at 19:20
  • 1
    There are already enough answers, that mentions, that PHP is not an OOP-language from the start. However, worth to mention, that in fact PHP is not a pure OOP-language right now ;) It declares itself a "multiparadigm language", that (in this context) means, that it doesn't want to pack everything into classes just because "it can" Commented Feb 23, 2012 at 19:24

6 Answers 6

2

It's because there weren't objects when PHP was first written. There are a number of libraries that wrap basic PHP functionality with OO. Much like MFC wrapps the basic C Windows API with C++ classes.

If you use a framwork like Kohana, it provides lots of those wrappers for you like a File Wrapper http://kohanaframework.org/3.0/guide/api/File

Here's Kohana full API http://kohanaframework.org/3.0/guide/api/

Sign up to request clarification or add additional context in comments.

Comments

1

PHP wasn't an OOP language until recently.

The real OOP appeared only in v5.x; all the old functions are left for the reasons of backwards compatibility, and why to invent something new when old is working well?

That said, for some libraries there are OOP alternatives; e.g. there is a PDO library which replaces the legacy procedure-style mysql library.

Comments

1

PHP originally was written as a simple way to add dynamic text to HTML pages--not a formal well defined OO language. Until php 5 OO was very limited. Zend (the php creators and maintainers) want to ensure a high degree of backward compatibility, since a large chunk of the web runs on php.

More and more functionality gets official OO wrapper classes with each new release, some via pecl or pear add ons. Good practice is to Use the OO version when possible.

I doubt if we'll ever see a 100% OO php to the degree of say Java.

Comments

1

Even I can understand part of your question, the example you give is actually a bit misleading, because that part is object oriented in PHP, see SplFileObject and friends:

$file = new SplFileObject("/path/to/file.txt");
$content = '';
foreach ($file as $line)
  $content .= $line;

There is more like the whole SPL, DOMDocument, DateTime, Intl, PDO and so on and so forth.

3 Comments

I only gave it as an example to demonstrate what I meant about using functions with handle parameters.
Well not everything must be object oriented, the handle example you give is often used in procedural code allowing to reference an "object". But PHP has changed a lot since 5 / 5.2 and you can find more and more object oriented interfaces nowaday. Many libraries have both, like Tidy but others, for example GMP.
Object orientation is (among other things) a wrapper that automatically passes the object as the first parameter to functions, and calls it 'this';
1

Ok, I am no expert at this, but I'm going to offer my $.02 of speculation anyway:

PHP, and web development in general, is a relatively new paradigm. C# is based on what, 40 years of work? PHP started in 1995.

PHP can be fully OO but programming for a web server is an environment that is naturally procedural - the server receives an HTTP request, it undergoes some processing, it replies. You can abstract away from this, but is it really a good idea all the time?

Many tasks in a web server environment don't really require OO - it's nice in big projects, but how many thousands of sites just use a couple string processing functions and include()? Maybe mail()?

PHP is built for speed, to run on lightweight servers, with an emphasis on string processing as opposed to advanced math and the like. Don't quote me, but I'm thinking you may incur some performance overhead if you went strictly OO with base classes.

Anyway, I'm no expert, but that's kind of my thinking on it :) Food for thought at least.

2 Comments

This seems to answer the question, "Should PHP's built-in libraries be object oriented?" rather than, "Why are PHP's built-in libraries not object oriented?"
Well, if you can reasonably assume that PHP is non-object-oriented by design, then they're pretty much the same question. I mean, the only way to get a definitive answer is to ask Zend, I was just offering my speculation on their logic behind making PHP the way it is.
0

I'm sure backwards compatibility will enter into the equation somewhere. Even if the core php developers recognized their "mistake" it's very hard to undo it since PHP is widely implemented. The date functions and the DateTime classes are a good example of PHP trying to improve its usage without breaking the upgrade path

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.