{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/5219","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/5219/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/5219/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/5219/events","html_url":"https://github.com/yiisoft/yii2/issues/5219","id":44224530,"number":5219,"title":"Suggestion for New Ordering Appoarch of Shutdown Functions","user":{"login":"IndeedPlusPlus","id":3715412,"avatar_url":"https://avatars.githubusercontent.com/u/3715412?v=3","gravatar_id":"","url":"https://api.github.com/users/IndeedPlusPlus","html_url":"https://github.com/IndeedPlusPlus","followers_url":"https://api.github.com/users/IndeedPlusPlus/followers","following_url":"https://api.github.com/users/IndeedPlusPlus/following{/other_user}","gists_url":"https://api.github.com/users/IndeedPlusPlus/gists{/gist_id}","starred_url":"https://api.github.com/users/IndeedPlusPlus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/IndeedPlusPlus/subscriptions","organizations_url":"https://api.github.com/users/IndeedPlusPlus/orgs","repos_url":"https://api.github.com/users/IndeedPlusPlus/repos","events_url":"https://api.github.com/users/IndeedPlusPlus/events{/privacy}","received_events_url":"https://api.github.com/users/IndeedPlusPlus/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/yiisoft/yii2/labels/feature:hhvm%20compatibility","name":"feature:hhvm compatibility","color":"5319e7"}],"state":"open","locked":false,"assignee":{"login":"cebe","id":189796,"avatar_url":"https://avatars.githubusercontent.com/u/189796?v=3","gravatar_id":"","url":"https://api.github.com/users/cebe","html_url":"https://github.com/cebe","followers_url":"https://api.github.com/users/cebe/followers","following_url":"https://api.github.com/users/cebe/following{/other_user}","gists_url":"https://api.github.com/users/cebe/gists{/gist_id}","starred_url":"https://api.github.com/users/cebe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cebe/subscriptions","organizations_url":"https://api.github.com/users/cebe/orgs","repos_url":"https://api.github.com/users/cebe/repos","events_url":"https://api.github.com/users/cebe/events{/privacy}","received_events_url":"https://api.github.com/users/cebe/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/yiisoft/yii2/milestones/9","html_url":"https://github.com/yiisoft/yii2/milestones/2.0.x","labels_url":"https://api.github.com/repos/yiisoft/yii2/milestones/9/labels","id":831720,"number":9,"title":"2.0.x","description":"","creator":{"login":"cebe","id":189796,"avatar_url":"https://avatars.githubusercontent.com/u/189796?v=3","gravatar_id":"","url":"https://api.github.com/users/cebe","html_url":"https://github.com/cebe","followers_url":"https://api.github.com/users/cebe/followers","following_url":"https://api.github.com/users/cebe/following{/other_user}","gists_url":"https://api.github.com/users/cebe/gists{/gist_id}","starred_url":"https://api.github.com/users/cebe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cebe/subscriptions","organizations_url":"https://api.github.com/users/cebe/orgs","repos_url":"https://api.github.com/users/cebe/repos","events_url":"https://api.github.com/users/cebe/events{/privacy}","received_events_url":"https://api.github.com/users/cebe/received_events","type":"User","site_admin":false},"open_issues":338,"closed_issues":2,"state":"open","created_at":"2014-10-19T13:49:45Z","updated_at":"2015-05-08T21:19:14Z","due_on":null,"closed_at":null},"comments":12,"created_at":"2014-09-28T09:30:52Z","updated_at":"2015-05-09T14:51:55Z","closed_at":null,"body":"It is known that nested `register_shutdown_function` calls can cause problem under HHVM and there were no fix for this currently. The official PHP document does not guarantee that any sub-sequence calls of `register_shutdown_function` can be made during shutdown.\r\n\r\nI suggest another approach to arrange the order of executions of shutdown functions by making use of the standard SplPriorityQueue class. Here is a simple example illustrating how this method works.\r\n```php\r\n<?php\r\nfunction register_shutdown_callback(callable $c, $weight = 0)\r\n{\r\n    static $queue = null;\r\n    if ($queue === null) {\r\n        /* The queue will not be created and register_shutdown_function will not be called */\r\n        /* if the function is not used. */\r\n        $queue = new SplPriorityQueue();\r\n        /*we need the weight as the only parameter for the shutdown callback, so extracting both.*/\r\n        $queue->setExtractFlags(SplPriorityQueue::EXTR_BOTH); \r\n        register_shutdown_function(function() use ($queue)\r\n        {\r\n            while ($queue->valid()) {\r\n                $arr  = $queue->current();\r\n                $callback = $arr['data'];\r\n                $priority = $arr['priority'];\r\n                call_user_func($callback, $priority);\r\n                $queue->next();\r\n            }\r\n        });\r\n    }\r\n    $queue->insert($c, $weight);\r\n}\r\n\r\n/* Some examples. */\r\nregister_shutdown_callback(function($weight)\r\n{\r\n    echo 'Hello, world!' . PHP_EOL;\r\n    register_shutdown_callback(function($weight)\r\n    {\r\n        echo 'Hello again.' . PHP_EOL;\r\n    }, $weight - 1);\r\n});\r\n\r\n/* This function is ensured to be called at last. */\r\nregister_shutdown_callback(function($weight)\r\n{\r\n    echo 'FINAL callback.' . PHP_EOL;\r\n}, -INF);\r\n```\r\n... and the output\r\n```\r\nHello, world!\r\nHello again.\r\nFINAL callback.\r\n```"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/100499569","html_url":"https://github.com/yiisoft/yii2/issues/5219#issuecomment-100499569","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/5219","id":100499569,"user":{"login":"klimov-paul","id":1482054,"avatar_url":"https://avatars.githubusercontent.com/u/1482054?v=3","gravatar_id":"","url":"https://api.github.com/users/klimov-paul","html_url":"https://github.com/klimov-paul","followers_url":"https://api.github.com/users/klimov-paul/followers","following_url":"https://api.github.com/users/klimov-paul/following{/other_user}","gists_url":"https://api.github.com/users/klimov-paul/gists{/gist_id}","starred_url":"https://api.github.com/users/klimov-paul/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/klimov-paul/subscriptions","organizations_url":"https://api.github.com/users/klimov-paul/orgs","repos_url":"https://api.github.com/users/klimov-paul/repos","events_url":"https://api.github.com/users/klimov-paul/events{/privacy}","received_events_url":"https://api.github.com/users/klimov-paul/received_events","type":"User","site_admin":false},"created_at":"2015-05-09T14:51:55Z","updated_at":"2015-05-09T14:51:55Z","body":"Of course we can not cover third party callbacks. But I don't think it is a thing we should worry about.\r\nWe can at least make Yii compatible with HHVM at this point and have other already mentioned benefits.\r\nWe can add a docs suggesting developers to use `Yii::registerShutDownFuntion()` instead of plain `register_shutdown_funtion()`. This will not affect independent libraries, but at least Yii itself will be more reliable."}}