Tuesday, July 17, 2012


New Object-Oriented Model:

       When Zeev Suraski added the object-oriented syntax back in the days of PHP 3, it was added as “syntactic sugar for accessing collections.” The OO model also had support for inheritance and allowed a class (and object) to aggregate both methods and properties, but not much more. When Zeev and Andi Gutmans rewrote the scripting engine for PHP 4, it was a completely new engine; it ran much faster, was more stable, and boasted more features. However, the OO model first introduced in PHP 3 was barely touched. Although the object model had serious limitations, it was used extensively around the world, often in large PHP applications. This impressive use of the OOP paradigm with PHP 4, despite its weaknesses, led to it being the main focus for the PHP 5 release.

      So, what were some of the limitations in PHP 3 and 4? The biggest limitation (which led to further limitations) was the fact that the copy semantics of objects were the same as for native types. So, how did this actually affect the PHP developer? When assigning a variable (that points to an object) to another variable, a copy of the object would be created. Not only did this impact performance, but it also usually led to obscure behavior and bugs in PHP 4 applications because many developers thought that both variables would point at the same object, which was not the case. The variables were instead pointing at separate copies of the same object. Changing one would not change the other.

For example:

class Person {
var $name;
function getName()
{
return $this->name;
}
function setName($name)
{
$this->name = $name;
}
function Person($name)
{
$this->setName($name);
}
}
function changeName($person, $name)
{
$person->setName($name);
}
$person = new Person("Andi");
changeName($person, "Stig");
print $person->getName();


          In PHP 4, this code would print out
 & signs.
         This behavior is not intuitive, as many developers would expect the Javalike behavior. In Java, variables actually hold a handle (or pointer) to the object, and therefore, when it is copied, only the handle (and not the entire
object) is duplicated.

        There were two kinds of users in PHP 4: the ones who were aware of this problem and the ones who were not. The latter would usually not notice this problem and their code was written in a way where it did not really matter if the problem existed. Surely some of these people had sleepless nights trying to track down weird bugs that they could not pinpoint. The former group dealt with this problem by always passing and assigning objects by reference. This would prevent the engine from copying their objects, but it would be a headache because the code included numerous

       The old object model not only led to the afore-mentioned problems, but also to fundamental problems that prevented implementing some additional features on top of the existing object model. In PHP 5, the infrastructure of the object model was rewritten to work with object handles. Unless you explicitly clone an object by using the
clone keyword, you never create behind-the-scenes duplicates of your objects. In PHP 5, you don’t need a need to pass objects by reference or assign them by reference.
"Andi" . The reason is that we pass the object $person to the changeName() function by-value, and thus, $person is copied and changeName() works on a copy of $person.

2 comments:

  1. Thanks for sharing this info and this is a big help and useful also. The php developer should read those information because they could get some ideas on this. Keep it up. :)

    ReplyDelete
    Replies
    1. Thanx.......

      If you need any help than contact me.

      Within these days i will share more knowledge on my blog.....

      Delete