📜 ⬆️ ⬇️

PHP does not like destructors

So much water has flowed since PHP proclaimed itself the PLO language. I wasn’t able to clarify exactly when, but “On July 13, 2004, PHP 5 was released, powered by the new Zend Engine II. PHP 5 included new features such as improved support for object-oriented programming. ”
That is, theoretically, even then designers and destructors appeared. I myself use version 5.3.2, but I wonder what it gets up to.


A little bit about yourself. I am a C ++ programmer, 4 years experience. Specialization - computer graphics and networking. Namely, the creation of online games. But such games need a server, and the server has a base of players. And the players also want a website. “Why hire a web programmer, I'm not bad myself. At the same time and learn the language. "
So I thought half a year ago, but still can not understand!

Classic

I think many who worked with destructors once encountered:
PHP Fatal error: Exception thrown without a frame in Unknown on line 0
The first reaction is bewilderment. The second matt. And after all, cracking exit () everywhere doesn’t work, because the guess that the breaker in the destructor does not come right away, and if it does, then the code base is most likely significant.
Answer c bugs.php.net/bug.php?id=33598
sniper@php.net:
Throwing exceptions in __desctruct() is not allowed.
Should be documented..
vrana@php.net:
Thank you for the report, and for helping us make our documentation better.
"Attempting to throw an exception from a desctructor causes a fatal error."

Fun? Personally, I do not really.
Reproducing an error with an implicit exception is very simple.
class a
{
// ...
public function __destruct()
{
global $_SESSION;
$_SESSION = "Some information";
}
}
$dummy = new a();

^ Note, explicit exceptions sometimes work correctly. I will put experiments that affects.
')
Reverse cleaning procedure

Let in some scope (global and non-very) we have the following code:
$earth = new world();
$vasya = new human($earth);

Accordingly, in the code of the constructor of a person, he goes to push the world towards him. (It is assumed that without the world Vasya will not exist, we will reject the philosophy, we rather close the project.)
Well, in the destructor code, Vasya is ala $ this-> my_world-> RemoveHumanFromWorld ($ this), in which are called RemoveAllMyStuff, CalculateKarma, and so on. (Suppose we do not keep a reference to Vasya in the world, since this was not required as part of the task)
What does pkhp do when it goes out of scope? Destroys the world and crashes with the error "Fatal error: Call to a member function RemoveHumanFromWorld () on / a / in / home / my_projects / earth / creat / reasonable / homo_sapiens.php on line 1956".
(Therefore, by the way, the world was written in C ++, because God doesn’t need it to run on any universe. Virtual space with a garbage collector. Ha-Ha.)
Reply with bugs.php.net/bug.php?id=36759
dmitry@php.net
Fixed in CVS HEAD and PHP_5_2.
To find this Dmitry and poke his nose like in that picture. I do not know how it is in the latest versions, until I update, but in 5.3 it is relevant.

Script Completion - Exception

Oh, so much you can write about. Global variables (ala sessions) are no longer valid, access to the file system is cut off. And in general, as I understand the correct operation of the script is not guaranteed. So God forbid you to do something in the global object destructor ...
But we omit it. The documentation of php 5.1 and earlier (stanza and verse will not find it) says: “This is in principle logical for a language without the strict requirement of the delete construction (lat. unset).
After the bugs.php.net/bug.php?id=38572 report bug
The documentation has changed, "There has been no reference during the shutdown sequence."
Conveniently? For me - not very.

Direct links only

Let, according to the logic of the language, the object $ a should leave before $ b.
But let the reference to the $ a-> some_data field be stored in the object $ b.
Then, logically, the object $ b should retire earlier. Alas, in pkhp not so. I did not find such a bug, but the situation is specific (and I’m not afraid of the word exceptional). It is avoided by a slight patch before the link to $ a, I did not tolerate it and reportable.

Dedlock in the style of pkhp

$ a-> ref = $ b;
$ b-> ref = $ a;
I was wondering at one time how php would handle cross-references. Does it hinge? Does the error "can not leave the scope", and how it will sound in English. Alas, the greenhouse showed - all variables will exist until the end, until they come as they say "or in any order during the shutdown sequence"

Conclusion

At the moment, everything that I remembered. And maybe all that I met. But the impression was made that destructors in PCP are a crutch, so most likely I will stumble again soon.
I think the future of OOP web programming for the c ++ interpreter, and perhaps it will be ready. Maybe someone will take one day to change with ++, add standard constructions so that it becomes web-oriented. But so far I have not found alternatives.

Links

en.wikipedia.org/wiki/PHP
php.net/manual/en/language.oop5.decon.php
Although what is there a little thing:
[http: //]. * php \ .net. *

Source: https://habr.com/ru/post/140762/


All Articles