Why doesn't PHP use objects in their libraries by default?

I have been writing in PHP for more than six months, and while I am far from an expert, I can easily get around and turn the scripts into everything I need. I come from an object-oriented background, and this is what PHP seems to use very little, if any, in its default libraries.


Most of the external libraries that I use or create work with an object-oriented design, while the following example is used by default. I will use the file reading / writing 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 it would be wiser for me to use a design that looks something like this:

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

Now I’m quite sure that there will be a certain argument for this, since the same idea applies to strings, arrays, cURL, MySQL queries, etc. I would be interested to know what it is.

So, if it is better to write separate functions, taking a descriptor or resource as the first parameter, for example.

 object_method($handle, $value); 

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

 $object->method($value); 

And what should I use when writing my own libraries and applications?

+4
source share
6 answers

This is because there were no objects when PHP was first written. There are many libraries that wrap basic PHP functions with OO. Just as MFC wraps the base C Windows API with C ++ classes.

If you use a framework like Kohana, it provides many wrappers for you like File Wrapper http://kohanaframework.org/3.0/guide/api/File

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

+2
source

Until recently, PHP was not an OOP language.

Real OOP appeared only in version v5.x; all old functions are left for backward compatibility reasons and why come up with something new when the old one works well?

However, for some libraries, OOP alternatives exist; for example, there is a PDO library that replaces the legacy procedure-style mysql library.

+1
source

PHP was originally written as an easy way to add dynamic text to HTML pages, rather than a formal, well-defined OO language. Until php 5 OO was very limited. Zend (creators and proponents of php) want a high degree of backward compatibility, since most web pages run on php.

More and more functions get official OO wrapper classes with each new version, some of them can be added via pecl or pear. It is good practice to use the OO version whenever possible.

I doubt someday see 100% OO-php in Java degree.

+1
source

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

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

More like SPL , DOMDocument , DateTime , Intl , PDO , etc. etc.

+1
source

Well, I'm not an expert on this, but I still offer my $ .02 spec:

PHP and web development in general are a relatively new paradigm. C # is based on the fact that 40 years of work? PHP started working in 1995.

PHP can be completely 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 responds. You can ignore this, but is it really a good idea all the time?

Many tasks in the web server environment do not really require OO - this is good in large projects, but how many thousands of sites use only the string processing and include () functions? Maybe mail ()?

PHP is built for speed, for working on lightweight servers, with an emphasis on string processing, unlike advanced math and the like. Do not quote me, but I think you can incur some performance overhead if you went strictly with OO with base classes.

In any case, I am not an expert, but I thought about it :) Food for thought at least.

+1
source

I'm sure backward compatibility will go into the equation somewhere. Even if the main php developers have acknowledged their "mistake", it is very difficult to undo, since PHP is widely implemented. Date functions and DateTime classes are a good example of PHP trying to improve its use without breaking the update path.

0
source

Source: https://habr.com/ru/post/1398107/


All Articles