2009-09-17 16 views

उत्तर

19

PHP सहायता डॉको से आप बाहर निकलने के बाद बुलाए जाने वाले फ़ंक्शन को निर्दिष्ट कर सकते हैं (लेकिन स्क्रिप्ट समाप्त होने से पहले।

अधिक जानकारी के लिए doco जांच करने के लिए स्वतंत्र महसूस http://us3.php.net/manual/en/function.register-shutdown-function.php

<?php 
function shutdown() 
{ 
    // This is our shutdown function, in 
    // here we can do any last operations 
    // before the script is complete. 

    echo 'Script executed with success', PHP_EOL; 
} 

register_shutdown_function('shutdown'); 
?> 
6

यदि आप ओओपी का उपयोग करते हैं तो आप उस कोड को डाल सकते हैं जिसे आप अपनी कक्षा के विनाशक में बाहर निकलने के लिए निष्पादित करना चाहते हैं।

class example{ 
    function __destruct(){ 
     echo "Exiting"; 
    } 
} 
3

आपका उदाहरण के रूप में यह आसानी से पुन: लिखा जा सकता है इस प्रकार है, बहुत साधारण शायद है:

if($result1 = task1()) { 
    $result2 = task2(); 
} 

common_code(); 
exit; 

शायद आप कोशिश कर रहे हैं इस तरह प्रवाह नियंत्रण बनाने के लिए:

do { 
    $result1 = task1() or break; 
    $result2 = task2() or break; 
    $result3 = task3() or break; 
    $result4 = task4() or break; 
    // etc 
} while(false); 
common_code(); 
exit; 

आपका भी उपयोग कर सकते हैं:

switch(false) { 
case $result1 = task1(): break; 
case $result2 = task2(): break; 
case $result3 = task3(): break; 
case $result4 = task4(): break; 
} 

common_code(); 
exit; 

या पीएचपी 5.3 में आप goto उपयोग कर सकते हैं:

if(!$result1 = task1()) goto common; 
if(!$result2 = task2()) goto common; 
if(!$result3 = task3()) goto common; 
if(!$result4 = task4()) goto common; 

common: 
echo "common code\n"; 
exit; 
संबंधित मुद्दे