Reputation: 29999
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?
Upvotes: 4
Views: 244
Reputation: 92294
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/
Upvotes: 2
Reputation: 3664
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.
Upvotes: 1
Reputation: 198115
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.
Upvotes: 1
Reputation: 32155
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
Upvotes: 0
Reputation: 41448
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.
Upvotes: 1
Reputation: 9912
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.
Upvotes: 1