-2
有一個問題,我想循環trought整個堆棧或停止某處,但不知何故循環拋出的而不是計數...項目值被錯誤地分配給真正的,該怎麼辦?運營商優先
php7
編輯::
好吧...我試圖使它抽象...
<?php
class foo {
private static $instance = [];
private $stack = [];
private $stop;
public static function i() {
if (!isset(static::$instance[0])) {
static::$instance[0] = new static();
}
return static::$instance[0];
}
public static function destroy() {
unset(static::$instance[0]);
}
public function stop() {
$this->stop = true;
}
private function __construct() {
echo '__constructor being called'.PHP_EOL;
$this->fillTheVoid();
}
public function do() {
echo 'BEGIN'.PHP_EOL;
$this->work();
echo 'END'.PHP_EOL;
}
private function fillTheVoid() {
$k = 0;
for($i = 0; $i < 20 ; $i++) {
$this->stack[] = ++$k;
}
}
private function work() {
while($item = array_pop($this->stack) && !$this->stop) {
echo $item.PHP_EOL;
}
}
}
$foo = foo::i();
$foo -> do();
function shutdown() {
echo 'shutting_down'.PHP_EOL;
$foo = foo::i();
$foo -> stop();
for($i = 3; $i > 0; $i--) {
sleep(1);
echo $i.PHP_EOL;
}
}
register_shutdown_function('shutdown', null);
exit;
?>
輸出是:
__constructor being called
BEGIN
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
END
shutting_down
3
2
1
不知怎的,這是循環錯誤
我們想要數,相反,我們剛剛得到的...
這起源與構造問題,但後來我無意中發現了語法錯誤,所以我改變一點點......
如果它真的是單身人士,不要擔心它會返回相同的對象,你不必擔心它 – shashi
你可以發佈單身人士代碼和關機功能嗎? – vincenth