After an Exception object is created, you can return the object, but it should not be applied in this way. a better way is to use the keyword throw instead. Throw is used to throw an exception: thrownewException ("mymessage", 44); throw interrupts script execution and
After an Exception object is created, you can return the object, but it should not be applied in this way. a better way is to use the keyword throw instead. Throw is used to throw an exception:
Throw new Exception ('My message', 44 );
Throw interrupts the execution of the script and makes the relevant Exception object available to the client code.
The getCommandObject () method is improved as follows:
Index_php5.php
<? Php
// PHP 5
Require_once ('command _ php5/Command. php ');
Class CommandManager {
Private $ Export Dir = 'command _ php5 ';
Function getCommandObject ($ cmd ){
$ Path = '{$ this-> Export Dir}/{$ cmd}. php ';
If (! File_exists ($ path )){
Throw new Exception ('could not find $ path ');
}
Require_once $ path;
If (! Class_exists ($ cmd )){
Throw new Exception ('class $ cmd does not exist ');
}
$ Class = new ReflectionClass ($ cmd );
If (! $ Class-> isSubclassOf (new ReflectionClass ('command '))){
Throw new Exception ('$ cmd is not a Command ');
}
Return new $ cmd ();
}
}
?>
In the code, we apply the Reflection API of PHP5 to determine whether the given class belongs to the Command type. If the script is executed in the wrong path, the following error will be reported:
Fatal error: Uncaught exception 'exception' with message 'could not find command/xrealcommand. php' in/home/xyz/BasicException. php: 10
Stack trace:
#0/home/xyz/BasicException. php (26 ):
CommandManager-> getCommandObject ('xrealcommand ')
#1 {main}
Thrown in/home/xyz/BasicException. php on line 10
By default, a fatal error occurs when an exception is thrown. This means that security mechanisms are built in the application exception class. Applying only one error mark does not have this effect. Failed to handle the error mark will only apply the error value to your script for continuous fulfillment.