note 123674 added to control-structures.declare
| From: | digitalaudiorock at gmail dot com | Date: | Sat, 09 Mar 2019 21:14:13 +0000 |
| Subject: | note 123674 added to control-structures.declare | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-210343@lists.php.net to get a copy of this message | ||
A few important things to note for anyone using this in conjunction with signal handlers:
If anyone is trying to optionally use either pcntl_async_signals() when available (PHP >= 7.1) or
ticks for older versions, this is not possible...at least not in a way that does NOT enable ticks
for newer PHP versions. This is because there is simply no way to conditionally declare ticks. For
example, the following will "work" but not in the way you might expect:
<?php
if (function_exists('pcntl_async_signals')) {
pcntl_async_signals(true);
} else {
declare(ticks=1);
}
?>
While signal handlers will work with this for old and new version, ticks WILL be enabled even in the
case where pcntl_async_signals exists, simply because the declare statement exists. So the above is
functionally equivalent to:
<?php
if (function_exists('pcntl_async_signals')) pcntl_async_signals(true);
declare(ticks=1);
?>
Another thing to be aware of is that the scoping of this declaration changed a bit from PHP 5.6 to
7.x...actually it was corrected apparently as noted here:
http://php.net/manual/en/function.register-tick-function.php#121204
This can cause some very confusing behavior. One example is with the pear/System_Daemon module. With
PHP 5.6 that will work with a SIGTERM handler even if the script using it doesn't itself use
declare(ticks=1), but does not work in PHP 7 unless the script itself has the declaration. Not only
does the handler not get called, but the signal does nothing at all, and the script doesn't
exit.
A side note regarding ticks that's annoyed me for some time: As if there wasn't enough
confusion around all this, the Internet is full of false rumors that ticks were deprecated and are
being removed, and I believe they all started here:
http://www.hackingwithphp.com/4/21/0/the-declare-function-and-ticks
Despite a very obscure author's note at the very end of the page saying he got that wrong (that
even I just noticed), the first very prominent sentence of the article still says this, and that
page is near the top of any Google search.
----
Server IP: 208.43.231.9
Probable Submitter: 82.199.157.34 (proxied: 216.151.180.131)
----
Manual Page -- http://php.net/manual/en/control-structures.declare.php
Edit -- https://master.php.net/note/edit/123674
Del: integrated -- https://master.php.net/note/delete/123674/integrated
Del: useless -- https://master.php.net/note/delete/123674/useless
Del: bad code -- https://master.php.net/note/delete/123674/bad+code
Del: spam -- https://master.php.net/note/delete/123674/spam
Del: non-english -- https://master.php.net/note/delete/123674/non-english
Del: in docs -- https://master.php.net/note/delete/123674/in+docs
Del: other reasons-- https://master.php.net/note/delete/123674
Reject -- https://master.php.net/note/reject/123674
Search -- https://master.php.net/manage/user-notes.php