Well, for example, there is a class:

class A { public $value = 100; } 

And there is a variable with an object instance:

 $obj = new A; 

As far as I heard that the variable is stored not quite an object but some kind of ID? For example, the following code:

 $cop = $obj; $ref = &$obj; $obj->value = 1; echo $obj->value," ",$cop->value," ",$ref->value,"\n"; /// 1 1 1 $obj = NULL; var_dump($obj,$cop,$ref); /// NULL object(A)#1 NULL 

Explain in the same way that it is stored in a variable which assigns an "object".

    2 answers 2

    To answer this question, one has to climb into the wilds of memory management and variable representations. Happiness adds that this view has changed quite a bit in PHP7 and the complex answer requires you to describe both models. The description of this kitchen in its minimum form has already required articles in two parts: the first , second . There is a translation into Russian: the first part , the second . Then the articles are about PHP5 objects ( translation ), device changes in PHP7 ( translation )

    Answers-references to so are not recommended, but this is a huge layer of material and even the basics in an understandable form, I cannot state the reasonable amount of the text, but I also do not consider it correct to suggest a specific question in this case.

    • Uhh, a lot of material, only the difficulty is that I have little knowledge of C - MaximPro
    • I did not read the last article, but what can I say from all that I read: 1. It’s very difficult to understand, the reducible code is almost incomprehensible. long and using underscores, the same goes for variable names. 3. From the three articles I read, it was found out that when writing the PHP engine C was used and the structure (it seems that there are no classes in C) 4. I didn’t understand much of it, but I looked at the picture so to speak ... C ++. - MaximPro
    • What do you think "what's better" to learn C or C ++, is not interested in terms of holivar, but in terms of utility as a whole. PS If you know at least one of the languages, then reading those articles will give a powerful insight, although the structure of the structures ... ahaha ... ahem is quite confusing. But even so poorly knowing C, I understood it. Not much, but I'm satisfied for now =) - MaximPro
    • In C, there are actually no classes, more than 40 years of language. A php under the hood - not the standard of pure code. C or pluses for utility in general ... I can not advise anything concrete, it is better both little by little. - Shallow
    • I found one book on C from Griffiths, I think the book is good, I would understand the base and indeed the general code in general, because I subsequently think in C / C ++ to write. PS I previously studied quite a bit C ++, I didn’t know C, which is why I wrote that (it seems that there are no classes in C), because in C ++ they mean, I remember. It was then when I studied C ++, I had a mess between classes and structures because I was far from it, although now it is not particularly close - MaximPro

    It stores a pointer to the created object.

     $obj = new A; // Создали объект, разместили его в памяти и // присвоили переменной $obj указатель на эту память $cop = $obj; // присвоили переменной $cop значение, хранящееся в $obj // т.к. в $obj хранился указатель на объект, то у этого // объекта увеличиваем счетчик ссылок на 1, чтобы объект не // разрушился досрочно // сейчас $cop и $obj хранят указатель, на один и тот же объект $ref = &$obj; // присваиваем $ref указатель на $obj, которая хранит указатель на объект // в $ref хранится указатель на указатель на объект $obj->value = 1; // изменили поле объекта echo $obj->value," ",$cop->value," ",$ref->value,"\n"; /// 1 1 1 // вывели три раза поле одного и того же объекта $obj = NULL; // присвоили $obj нулевой указатель и тем самым "отвязали" его от объекта. // У объекта счетчик ссылок уменьшаем на 1. // Т.к. текущее значение счетчика 1 ($cop еще ссылается на объект), // то объект не уничтожаем var_dump($obj, $cop, $ref); /// NULL object(A)#1 NULL // $obj мы обнулили сами, // $cop ссылается на живой объект, // $ref ссылается на $obj и принимает то же самое значение, что и $obj 
    • In general, right ... but still blurry and not accurate. But in any case, I think that your answer is useful (I will vote) - MaximPro