0

i have the following logic in my model:

if ( $switch_obj->connect() )  {
  if ( $data = $switch_obj->showIntAll() )  {
        $switch_obj->disconnect();  
        return $data;
  }
  else  {
     $switch_obj->disconnect();
     throw new Exception('Empty Data Set');
  }
}
else  {                                     
   throw new Exception('Connection');                                  
    }

This switch_obj that's being called has logic in it's constructor and destructor to increment / decrement counters respectively. (saved in a class called testclass). So each time an object of type testclass is instantiated, a counter is increased. And then when destroyed, it's decremented. However, I've just discovered a scenario that I'm not handling.

Fatal error: Call to undefined method testclass::showIntAll() in /var/www/myapp/application/models/test_model.php on line 215

It's clear that I'm calling a method that doesn't exist, which I will resolve. But my question is this: in creating this error, i can see that the counter has already been incremented ... but not decremented because once this error is thrown, it never returns to the destructor method in my class. How would I program for these types of scenarios? Obviously, in production, I won't get getting errors because of missing methods in testclass... but in case I do get an unexpected error where the testclass constructor is called and then it bombs, I'm just wondering what the best way is to handle this.

4

2 に答える 2

2

register_shutdown_functionで何かを達成できるかもしれません。コンストラクターは、エラーが発生した場合に呼び出されるクリーンアップ関数を登録できます。ただし、クリーンアップ コードを 2 回呼び出さないように注意する必要があります (1 回はデストラクタから、もう 1 回はこの登録済み関数から)。

きれいな解決策ではありませんが、うまくいく可能性があります:)

于 2013-01-15T14:25:35.953 に答える
1

私が覚えている限り、デストラクタは致命的なエラーで呼び出されません

于 2013-01-15T14:24:10.297 に答える