PHP - using a namespace in another namespace -
i'm trying use files namespace inside 1 of own namespaces, it's not recognising exceptions defuse\crypto
namespace.
i've checked files, , there stored in directory. can include autoloader no exceptions too.
what can cleanly use both namespaces in same file?
here's code:
namespace defuse\crypto; $path = '/my/path/to/defusecrypto/autoloader'; require_once $path; use \defuse\crypto\crypto; use \defuse\crypto\exception ex; namespace mynamespace; class myclass { static function encrypt_key($key) { try { $ciphertext = crypto::encrypt($key, $privatekey); return $ciphertext; } catch (ex\cryptotestfailedexception $ex) { return false; } catch (ex\cannotperformoperationexception $ex) { return false; } } static function decrypt_key($key) { try { $decryptedkey = crypto::decrypt($key, $privatekey); return $decryptedkey; } catch (ex\invalidciphertextexception $ex) { return false; } catch (ex\cryptotestfailedexception $ex) { return false; } catch (ex\cannotperformoperationexception $ex) { return false; } } }
three options: either specify qualified (so absolute) namespace directly:
catch (\defuse\crypto\exception\cryptotestfailedexception $ex)
or use relative namespace inside active namespace:
catch (exception\cryptotestfailedexception $ex)
or have declare namespace under alias internal usage @henris. suggested above:
use defuse\crypto\exception ex; [...] catch (ex\cryptotestfailedexception $ex)
in general nothing speaks against using multiple namespaces in 1 file, common. agree @henris. here not practice create separate namespace exceptions.
Comments
Post a Comment