mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Autoload and exceptions work. Mostly. Fix#50250
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@291300 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
4ce57561b7
commit
0d2968f8e0
1 changed files with 67 additions and 3 deletions
|
@ -17,9 +17,13 @@
|
|||
</para>
|
||||
<note>
|
||||
<para>
|
||||
Exceptions thrown in __autoload function cannot be caught in the
|
||||
<link linkend="language.exceptions">catch</link> block and results in
|
||||
a fatal error.
|
||||
Prior to 5.3.0, exceptions thrown in the __autoload function could not be
|
||||
caught in the <link linkend="language.exceptions">catch</link> block and
|
||||
would result in a fatal error. From 5.3.0+ exceptions thrown in the
|
||||
__autoload function can be caught in the <link linkend="language.exceptions">
|
||||
catch</link> block, with 1 proviso. If throwing a custom exception, then
|
||||
the custom exception class must be available. The __autoload function may
|
||||
be used recursively to autoload the custom exception class.
|
||||
</para>
|
||||
</note>
|
||||
<note>
|
||||
|
@ -82,6 +86,66 @@ Fatal error: Interface 'ITest' not found in ...
|
|||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
<example>
|
||||
<title>Autoloading with exception handling for 5.3.0+</title>
|
||||
<para>
|
||||
This example throws an exception and demonstrates the try/catch block.
|
||||
</para>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
function __autoload($name) {
|
||||
echo "Want to load $name.\n";
|
||||
throw new Exception("Unable to load $name.");
|
||||
}
|
||||
|
||||
try {
|
||||
$obj = new NonLoadableClass();
|
||||
} catch (Exception $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
Want to load NonLoadableClass.
|
||||
Unable to load NonLoadableClass.
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
<example>
|
||||
<title>Autoloading with exception handling for 5.3.0+ - Missing custom exception</title>
|
||||
<para>
|
||||
This example throws an exception for a non-loadable, custom exception.
|
||||
</para>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
function __autoload($name) {
|
||||
echo "Want to load $name.\n";
|
||||
throw new MissingException("Unable to load $name.");
|
||||
}
|
||||
|
||||
try {
|
||||
$obj = new NonLoadableClass();
|
||||
} catch (Exception $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
Want to load NonLoadableClass.
|
||||
Want to load MissingException.
|
||||
|
||||
Fatal error: Class 'MissingException' not found in testMissingException.php on line 4
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
|
||||
<simplesect role="seealso">
|
||||
|
|
Loading…
Reference in a new issue