Noul Model de Obiecte

În PHP 5 este un nou Model de Obiecte. Manipularea obiectelor în PHP a fost complet rescrisă, îmbunătățind performanța și introducând mai multe facilități. În versiunile precedente ale PHP obiectele erau manipulate ca tipuri primitive (ca de exemplu întregi sau string-uri). Neajunsul acestei metode era că din punct de vedere semantic întregul obiect era copiat atunci când era atribuit unei variabile, sau când era transmis ca parametru unei metode. În noua abordare, obiectele sunt accesate prin handle, și nu prin valoare (puteți să considerați handle ca un identificator al obiectului).

Mulți programatori PHP nici nu-și dau seama despre trucurile cu copierea în modelul de obiecte vechi, de aceea majoritatea aplicațiilor PHP vor lucra fără modificări, sau cu foarte puține modificări.

Noul Model de Obiecte este documentat în Prezentarea Limbajului.

În PHP 5, funcția cu denumirea clasei este numită constructor, dar numai dacă este definită în această clasă. În PHP 4, ea este apelată de asemenea dacă este definită în clasa-părinte.

Vedeți de asemenea directiva zend.ze1_compatibility_mode pentru informații despre compatibilitatea cu PHP 4.

add a note add a note

User Contributed Notes 3 notes

up
1
bdas at premiergroup dot uk dot com
16 years ago
Since PHP5 upgraded PHP to an OOP language, they CHANGED the metaphor so that when you copy an object, you just get a pointer to it (as in C# and Java) and so therefore they needed to make a way to CLONE objects as well in case you need a REAL copy of the object.

Most cases, clone is not needed, simply because a real copy of an object is usually not mandatory.  In special cases, object cloning can be used to save time in porting.
up
0
quinn at strangecode dot com
17 years ago
Here is another possible solution for migrating code to php 5 when using $this = 'something' reassignments. In my case, I had several classes  with methods that were self-instantiating with static calls. I was able to simply use a different variable: I changed $this to $_this and it worked the same because I copied an instance of the original object by reference using an instantiation factory method:

class DB {
    function &getInstance()
    {
        static $instance = null;

        if ($instance === null) {
            $instance = new DB();
        }

        return $instance;
    }
    ...

In every method needing access to this object I assigned it to a temporary variable by reference:
   
    function doSomething ()
    {
        $_this =& DB::getInstance();

        $_this->doSomethingElse();
        $_this->param['id'] = 123;
    }

Which allows method calls or saving data back to the original object.

I originally created classes like this so I didn't need to keep track of instantiations or global objects. I could just call DB::doSomething() and the object is created dynamically or referenced from an already existing object.
up
-22
zzo38
16 years ago
You should be able to clone a object in compatibility of PHP4,PHP5 with:
<?php
$x
=unserialize(serialize($y));
?>
To Top