Development objective orientirovannosti PHP
One of the main components of planned 5-th version PHP becomes Zend Engine 2.0, supporting completely new model of object-oriented programming. This clause{article} describes development of support of object-oriented programming in PHP, including new opportunities and the changes planned in PHP 5.
How all this began?
About it know the few but when that today is known as PHP, only was formed by summer of 1997, - it was not planned, that it will have any object-oriented opportunities. Andi Gutmans and I worked above creation of the powerful, reliable and effective web-language based mainly on PHP/FI 2.0 and syntax of language C. In effect, we were far enough from any intentions concerning classes or objects is there should be simply structured language. However, in one of those years{summer} nights, on August, 27 all has changed.
Classes have been added in a code becoming a basis of version PHP 3.0. They have been added as a syntactic ornament for the organization of access to data sets. PHP already supported concept of associative files, and the added innovation was anything other, as in the new unusual way of access to similar sets. Nevertheless, as has shown time, this new syntax rendered much more serious influence on PHP, than was planned initially.
One more unknown fact for the majority is that in poru official occurrence PHP 3.0 in the middle of 1998 when he amazing rates gained in strength, Andi Gutmans'om and me has already been solved to copy realization of language. PHP could like users in an existing kind (actually, we knew, that he is pleasant to them) but as founders of the engine we knew that is created under a hood, and we could not be reconciled with it. The copied code which later has received a nickname ' Zend Engine ' (Zend is combination Zeev and Andi), has begun and began one of the basic components of the second reorganization whom has gone through PHP for the period hardly more than year.
Nevertheless, this reorganization has left objective model PHP, generally, not changed with version 3 - she still was simplified. Objects till now appreciably were a syntactic ornament for associative files and did not give users of enough of additional opportunities.
Objects during former times
So, what we could do{make} with objects in days of PHP 3.0 and even in current version PHP 4.0? Actually, - the little. Objects were as a matter of fact storehouses of properties, like associative files. The greatest difference was that objects should belong to any class. Classes, as well as in other languages, contained a set of properties and methods (functions), and copies of objects could be created from them with the help of the operator new. The individual inheritance allowing users to expand (or to narrow) frameworks of an existing class needlessly was supported to write a class nanovo or to create his copy. At last, PHP 4.0 also has added an opportunity to cause methods of the set class both in a context of use of object, and outside of it .
One of the major turning points in history PHP was that, despite of very much limited functionality and weight of problems and the restrictions, object-oriented programming in PHP prospered and became the most popular paradigm of increasing number of the finished PHP-applications. This tendency, former generally unexpected, has put PHP in unprofitable position. That fact started to be shown, that objects behaved not as in others OO languages, and as associative files.
Restrictions of former objective model
The most problematic party of object-oriented model PHP 3 / PHP 4 was that objects were passed on value, instead of under the link. What is it means?
Let's say, you have the simple, a little bit useless function named myFunction ():
<? php
function myFunction ($arg) {
$arg = 5;
}
?>
And you cause this function:
As you, probably, know, a call myFunction () will not change $myArgument; transferred{handed} in myFunction () is a copy of value $myArgument, instead of a variable $myArgument. This way of transfer of argument is called as transfer of arguments on value. Transfer of arguments under the link {In my opinion, is a typing error; I think, was present in a kind - " on value ".} it is realized almost in all structured languages and it is extremely useful, as allows you to write the or to cause another's functions, not worrying about by-effects which they can render on "external" variables for them.
However we shall consider the following example:
<? php
function wed ($bride, $groom) {
if ($bride-> setHusband ($groom);
$groom-> setWife ($bride)) {
return true;
} else {
return false;
}
}
wed ($joanne, $joe);
print areMarried ($joanne, $joe);
?>
(Realizations Woman:: setHusband (), Man:: setWife () and areMarried () are lowered{omitted} as exercise to the reader).
{wed - "to marry", bride - "bride", groom - "groom", husband - "husband", wife - "wife", areMarried - " they are married? "}
What will return areMarried ()? It is possible to hope, that two newly married will manage to remain married, at least, up to the following line of a code, but as you could guess, - will not stay. areMarried () will confirm, that they have divorced, as soon as married. Why?
The reason is simple. That objects in PHP 3.0 and 4.0 are not something especial and behave as any other variables, - when you pass $joanne and $joe in wed (), actually you pass not them. Instead of it, you pass their exact copies, duplicates. Thus, though their copies also marry in wed (), valid $joe and $joanne remain on safe distance from sacrament of a sacred marriage{spoilage}, in the protected external area of visibility.
Certainly, PHP 3 and 4 enable you to pass compulsorily variables under the link, allowing, thus, to functions to change the arguments transferred{handed} by him from external area of visibility. If we have defined{determined} the prototype wed () so:
<? php
function wed (both $bride, and $groom)
?>
That for Joanne and Joe all would develop more successfully (or less, depending on yours on that a sight).
However, all is much more complex . For example, what if you want to return object from function under the link? That if you want to make changes in $this inside the designer, not worrying what can take place when they as a result of performance of the operator new will be copied in the variable - container? Do not know, about what I?.. Say "alleluia" {and better read section References inside the constructor <http://www.php.net/manual/en/language.oop.newref.php> from PHP Manual}.
In spite of the fact that PHP 3 and 4 in the certain degree coped with these difficulties, giving syntactic shifts for transfer of objects under the link, they never undertook essence of a problem:
Objects differ from other kinds of values, hence, objects should be passed under the link if not it is specified other.
The decision - Zend Engine 2
When we, at last, were convinced, that objects - is valid creations special and deserve special behaviour, it became only a first step. We should offer such way of realization of it which will not affect other semantics PHP and, it is desirable, will not force to copy all PHP. Fortunately, the decision has come as the ray of light which has flashed above head Andi Gutmans'b hardly more of year back. His idea has consisted in replacement of objects with descriptors of objects {in the original - object handles}. Descriptors of objects, in essence, will be numbers, indexes in the global table of objects. To any other kinds of variables they will be similarly passed and come back on value. Due to this new intermediate level, now we shall work with descriptors of objects, instead of with objects. In effect, it means, that PHP will behave so as if objects are passed under the link.
Let's return to Joe and Joanne. How the behaviour wed () now will change? First, $joanne and $joe will not be any more objects, and become descriptors of objects, say, 4 and 7 accordingly. These integer descriptors will specify cells in a certain global table of objects where there are present{true} objects. When we shall pass them in wed (), local variables $bride and $groom will receive values 4 and 7; setHusband () will change object to which refers 4; setWife () will change object to which refers 7; and when wed () will finish performance, $joanne and $joe will already live first of the staying days of a joint life.
What is it all means for end users?
Thus, the end of a fairy tale now more idyllic, but what is it means for writing on PHP? It means some things. First, it means, that your applications will be carried out faster as will be much less operations of copying of the data. For example, when you pass $joe in function, instead of necessity to create the duplicate and to copy his name, date of a birth, a patronymic, the list of former addresses, number{room} socobespechenija and... What there still? - PHP should pass only one descriptor of object, a single whole number. Certainly, direct result of all it also is the economy of a significant memory size - storage of integers demands much less a place, than storage of all object entirely.
But, perhaps, more important is that the new objective model does{makes} object-oriented programming on PHP considerably more powerful and intuitive. You should not be confused to mysterious symbols any more and to carry out a problem . You should not care of that any more, whether will go through the changes brought by you in object inside the designer, behaviour of the operator directing horror new. About two nights any more never will not be necessary to remain, tracing imperceptible mistakes! Well - good, probably about the last I also have told a lie, but if it is serious, the new objective model very considerably reduces quantity{amount} of mistakes of a kind "catch - do-nochi", connected to objects. In turn, it means, that suitability of use PHP for large-scale projects becomes much easier for proving.
What the newcomer?
As it was possible to expect, Zend Engine 2 contains very many other properties agreeed to his new objective model. Some from them improve object-oriented opportunities, for example, private members and methods, static variables and aggregation at a level of language. Most significant - a revolutionary level of interaction with external models of components, such as Java, COM/DCOM and .NET by means of an overload.
In comparison with Zend Engine 1 in PHP 4.0 which for the first time has entered this kind of the integration, new realization is much faster, is more completed, more reliabl and even more easy in support and expansion. It means, that PHP 5.0 it will be very good to cooperate with your system on basis Java or .NET as you can use existing components in PHP obviously as though they were usual PHP-objects. As against PHP 4.0 which had special realization for such overloaded objects, PHP 5.0 uses the same interface for all objects, including native PHP-objects. This opportunity guarantees, that PHP-objects and the overloaded objects behave absolutely equally.
At last, Zend Engine 2 also brings in PHP processing of exceptions. Till now the sad validity is that the majority of developers write a code not gracefully enough processing erroneous situations. Not seldom there are the sites which are throwing out in your browser mysterious mistakes of a database instead of show of correctly formulated message " there Was such mistake ". In a case with PHP a principal cause of it that processing of erroneous situations - the problem resulting{bringing} in despondency; you, actually, should check returned value for all and for each function. With addition set_error_handler () to cope with this problem it became easier, as the opportunity to centralize processing of mistakes has appeared, but up to the desirable decision remained still far. Addition of processing of exceptions in PHP will enable developers to catch mistakes fineer net, and, that is more important, will promote elegant restoration after mistakes in what a place of the program they have taken place.
The conclusion
Version PHP 5.0 based on Zend Engine 2.0, will mark a significant step forward in development PHP as one of the basic for today web-platforms in the world. Saving the firm obligations to the users, preferring to use functionally structured syntax PHP, the new version will provide huge jump forward for those who is interested in his object-oriented opportunities - especially for the companies developing large-scale applications.

|