Results 1 to 3 of 3

Thread: Development of object oriented PHP

  1. #1
    Senior Member
    Join Date
    Sep 2010

    Development of object oriented PHP

    Development of object oriented PHP
    One of the main components which are planned within 5th PHP-version is going to be Zend Engine 2.0 supporting the newest model of object oriented programming. This article describes development of object oriented programming support in PHP including the new abilities and changes which are planned in PHP5.
    How did it all start?

    Only few know about it but when the thing that we know as PHP started its forming in summer 1997 it wasn�t planned that it will possess any object oriented abilities. The developers worked over creation of a powerful, reliable and effective web-language based mostly on PHP/FI 2.0 and on the syntax of language C. In fact, they had no intentions concerning classes and objects � it had to be just a structured language. But suddenly everything changed.
    Classes have been added into the code which became the base for PHP 3.0 version. They were added as syntactical decoration to organize access to data sets. PHP has already supported the idea of associative massives and the innovation added was nothing but a new unusual way of access to such sets. Nevertheless this new syntax proved to have more influence on PHP as it was initially planned.
    Another unknown for the most fact is that it was decided to rewrite realization of the language by the time of official release of PHP 3.0 in the middle of 1998 when it was developing at a great speed. Users could like PHP in the form it existed (in fact, they really liked it) but its creators knew what was happening inside of it and couldn�t bare this. The rewritten code which later on was called �Zend Engine� started the reconstruction which was done with PHP in a period of a little more than a year.
    Nevertheless this reconstruction left object model of PHP mostly without changes compared to the version 3 � it has always been simplified. Up to the moment objects have been mostly a syntactical decoration for associative massives and haven�t presented sufficient number of optional abilities to users.
    Objects in the previous times

    So what could we do with objects in PHP 3.0 and even in the current version of PHP 4.0? In fact, we couldn�t do much. Objects actually were the depository of properties like associative massives. The most considerable difference was that objects had to belong to some class. Like in other languages classes contained a set of properties and methods (functions) and specimens of the objects could be created of them by means of operator �new�. Single inheritance was supported enabling users to extend (or to constrict) frames of the class existing without necessity to rewrite a class or to create its copy. And finally PHP 4.0 has also added the ability to call methods of the set class in the context of object�s usage and outside of it as well.
    One of the most important turning points in the history of PHP was that in spite of limited functionality and many problems and restrictions object oriented programming flourished in PHP and became the most popular paradigm for the increasing number of accomplished PHP-applications. This tendency which was mostly unexpected put PHP into a rather unprofitable position. The fact became apparent that objects behaved not as in other object oriented languages but as associative massives.
    Restrictions of the previous object model

    The side which caused the most problems within object oriented model of PHP 3 / PHP 4 was transmitting objects on value instead of transmitting them by a link. What does it mean?
    For example, you have a simple and a little bit useless function called myFunction():
    function myFunction($arg) {
    $arg = 5;
    and you appeal this function:
    = 7;
    As you should know appealing myFunction() won�t change $myArgument transmitted in myFunction() � this is a copy of value $myArgument, not the variable $myArgument itself. This method of argument transmission is called arguments transmission on value. It is realized nearly in all structured languages and is rather useful as far as it enables you writing your own and calling others� functions without worrying about side effects which they may have upon the �external� for them variables.
    However we�ll examine the following example:
    function wed($bride, $groom) {
    if (
    $groom->setWife($bride)) {
    } else {
    wed($joanne, $joe);
    areMarried($joanne, $joe);
    (Realizations Woman::setHusband(), Man::setWife() and areMarried() are omitted as an exercise for a reader).
    { wed, bride, groom, husband, wife, areMarried }
    What returns areMarried()? You may hope that the two just married will succeed in staying married at least to the next string of the code but, as you can guess, they won�t manage it. areMarried() confirms that they divorced immediately after marriage. Why?
    The reason is simple. Because objects aren�t something especial in PHP 3.0 and 4.0 and behave like any other variables � when are transmitting $joanne and $joe into wed() you don�t transmit them in fact. Instead of them you transmit their exact copies, their duplicates. Thus although their copies marry in wed() real $joe and $joanne are far enough from being married in their protected external scope.
    Of course PHP 3 and 4 offer you a possibility to force variables to change on link enabling functions to change arguments transmitted to them from the external scope. If we defined prototype wed() as:
    function wed(&$bride, &$groom)
    everything would be much better for Joanne and Joe (or worse, depending on your point of view).
    However everything is much more complicated. And what if you want, for example, to return an object from function via link? What if want to do some changes in $this within a constructor without worrying about what could happen if new would be copied into container variable as a result of operator�s accomplishment? Don�t you know what I�m talking about? You�d better read unit �References inside the constructor� from PHP Manual.
    In spite of the fact that PHP 3 and 4 managed these difficulties somehow by offering some syntactical tricks for objects transmission on a link they have never got down to the matter of the problem:
    Objects differ from other value types and so they are to be transmitted on a link if nothing different is indicated.
    Solution � Zend Engine 2

    When we�ve finally made sure that objects are really very special creatures and deserve special behavior, it was just a first step. We had to offer such way of realization of this that wouldn�t have any influence upon the rest of PHP-semantics and won�t make us to rewrite the whole PHP. Fortunately the solution came a little more than a year ago. It consisted in replacing objects with object handles. In fact, object handles will be numbers, indexes within global table of objects. Like any other types of variables they will be transmitted and returned on value. Due to this new intermediate level now we are going to deal with object handles, not with objects themselves. In fact this means that PHP is going to behave as if objects themselves were transmitted on a link.
    Let�s return to Joe and Joanne. How will change behavior of wed() now? Firstly $joanne and $joe won�t be objects any more, they become object handles, for example, 4 and 7 correspondingly. These integer-valued handles will indicate columns within some global table of objects where the real objects are placed. When we transmit them into wed() local variables $bride and $groom get values 4 and 7; setHusband() changes the object to which 4 refers; setWife() changes the object to which 7 refers; and when wed() finishes accomplishment $joanne and $joe will already run through the first day of the rest of their lives with each other.
    What does this all mean for the end users?

    Thus the end of the story is more idyllic now but what does this mean for the writing in PHP? This means some things. Firstly your applications will be accomplished faster as far as there will be less data copying operations. For example, when you transmit $joe into function instead of necessity to create a duplicate and copy his name, date of birth, surname, list of previous addresses, the number of social service and� anything else? � PHP simply transmits only one object handle, one whole number. Of course, all this result directly into economy of considerable memory value � saving whole numbers needs much less place than saving of the whole object.
    But maybe the more important thing is that the new object model makes object oriented programming in PHP considerably more powerful and intuitive. You shouldn�t be confused with mysterious symbols & to accomplish a task any more. You shouldn�t worry about that if the changes made by you in the object inside of constructor will survive after the scaring behavior of operator new. You won�t have to sit up to the midnight observing difficult to catch errors! Well, maybe I was wrong with the last one but, speaking seriously, the new object model considerably reduces number of errors like �catch-up-to-the-midnight� connected with objects.
    What�s new?

    As it could be expected, Zend Engine 2 contains enough other properties corresponding with its new object model. Some of them improve object oriented abilities such as, for example, private members and methods, static variables and aggregation on the language level. The most important thing is the brand new level of interaction with external component models such as Java, COM/DCOM and .NET by means of reloading.
    Compared with Zend Engine 1 the new realization in PHP 4.0 which has firstly presented this kind of integration occurs considerably faster and is more reliable and easier to attend and extend. This means that PHP 5.0 will interact very well with your system based on Java or .NET as far as you�ll be able to use the already existing components in PHP explicitly as if they were ordinary PHP-objects. Unlike PHP 4.0 which included special realization for such reloaded objects PHP 5.0 uses the same interface for all the objects including original PHP-objects. This ability guarantees that PHP-objects and reloaded objects behave completely in the same way.
    And finally, Zend Engine 2 also brings exceptions� handling in PHP. Up to the moment the disappointing reality is the fact that the most developers write such codes which handling of error situations isn�t elegant enough. We often see such sites which transmit to our browser mysterious errors of data base instead of showing up the correct formulated message �Some error�. In case of PHP the main reason of this is the fact that handling of error situations is a rather depressing task; in fact, you should check returned value for all and for each function. With adding set_error_handler() dealing with this problem became easier as far as we�ve got a possibility to centralize error handling but the desirable solution still remained unachievable. Adding exceptions handling into PHP will developers the opportunity to catch errors with a finer filter and (which is more important) will also favour elegant reconstruction after the errors independent from the part of the program in which they happened.

  2. #2
    Junior Member
    Join Date
    Oct 2010
    Adopting object-oriented techniques when developing your PHP scripts and applications can immediately inject greater flexibility and easier maintenance into your development environment. Programs become easier to extend, or debug, and sharing code with other developers on your team (should you have one) becomes simpler.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Suppose you develop some software now and deploy it, and your business comes to depend on that software.

Similar Threads

  1. Replies: 1
    Last Post: 09-05-2011, 01:58 AM
  2. Object-oriented programming. Abstract classes in database
    By Meer in forum Database Administration
    Replies: 0
    Last Post: 10-20-2010, 09:44 AM
  3. New object model
    By Meer in forum Programming
    Replies: 0
    Last Post: 10-20-2010, 09:27 AM
  4. Replies: 0
    Last Post: 10-19-2010, 07:04 AM
  5. What is JSON (JavaScript Object Notation)
    By AP admin in forum Javascript and js libraries
    Replies: 0
    Last Post: 07-30-2010, 08:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts