开发者

Migrating PHP4 to 5 and handling Exceptions

开发者 https://www.devze.com 2023-02-06 06:00 出处:网络
I\'m migrating a legacy app from PHP4 to PHP5.2, and one of the libraries it uses is now throwing exceptions which cause the scripts to break. Am I correct in thinking that under PHP4 this e开发者_C百

I'm migrating a legacy app from PHP4 to PHP5.2, and one of the libraries it uses is now throwing exceptions which cause the scripts to break. Am I correct in thinking that under PHP4 this e开发者_C百科xception wouldn't be handled in the same way (i.e. not at all) and so the script would just continue on from the point the exception is now thrown? Is there any way to restore this behaviour under PHP5 (ideally just for the pages where the library is included)? The exception in question is a com_exception from a library dealing with Excel.


I have tried all binary PHP distributions for Windows that I found from 5.1.2 to 5.1.3 (cvs also) and i had to handle the exception.

My PHP setup is the typical one described in 'install.txt' of the PHP package. The only change that I have made in php.ini (except of the doc_root) is 'com.allow_dcom = true'.

so u will have to use

catch( com_exception $e )
{
print( $e->__toString( ) );
}


PHP 4 didn't support exceptions at all. The syntax to throw an exception would have been invalid syntax in PHP 4. See http://uk3.php.net/manual/en/language.exceptions.php

It would be handy to have been told a bit more about the class that is throwing the exeption.

My guess is that there's a bugm either in the class itself or in the way you're calling it which was being triggered all along and was being ignored, but is now causing this exception to be thrown.

This page is about the changes in PHP5, and has some insights into how the com_exception works: http://devzone.zend.com/article/762

To quote:

PHP 5 introduces structured exception handling (try, catch() and throw()) and this allows us to expose the underlying COM exceptions to PHP using the built-in com_exception class. If you want to catch errors in your scripts, you might write code like this:

<?php
$com = new COM("...");
try {
  $com->call_a_method();
}
catch (com_exception $e) {
  print $e . "\n";
}
?>

I hope the above code snippet helps you. Printing the error message may (should) reveal more about what exactly is going wrong, which should help you work out where to go from there.

It is possible, of course, that the Excel class you're using may also need to be upgraded; if it was part of your old PHP4 app, then presumably it's quite an old class; there may be a newer version of it available, and if you have found a bug in the class, then a new version may deal with that.

0

精彩评论

暂无评论...
验证码 换一张
取 消