새로운 객체 모델

PHP 5에서 새로운 객체 모델이 존재합니다. PHP의 객체 조작은 완전히 새로 쓰여졌으며, 더 나은 성능과 많은 기능을 허용합니다. 이전 버전의 PHP에서, 객체는 기본형처럼(정수형이나 문자열 같이) 다루었습니다. 이런 방식의 단점은 변수가 할당되거나, 메쏘드의 인수로 넘겨질 때 모든 객체를 복사해야 했습니다. 새로운 방식으로, 객체는 값이 아닌 핸들을 참조하게 됩니다. (핸들을 객체 식별자로 생각할 수도 있습니다)

많은 PHP 프로그래머는 이전 객체 모델의 복사에 대해서 크게 신경 쓰지 않았기 때문에, 대부분의 PHP 어플리케이션은 매우 작은 변경만으로도 작동합니다.

새 객체 모델은 언어 레퍼런스에 문서가 있습니다.

PHP 5에서 클래스 이름을 가진 함수는 같은 클래스 안에서 정의되었을 때에만 생성자로 호출됩니다. PHP 4에서는 정의된 클래스가 부모 클래스였어도 호출됩니다.

PHP 4와 호환을 위해서 zend.ze1_compatibility_mode 지시어를 참고하십시오.

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