|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2009-07-06 23:28 UTC] shiva dot buzz at gmail dot com
 Description:
------------
 I ran a small example for namespace. I found one Fatal error when the 
namespace was been used.
If you pass a invalid email id, that is  'alejandrodomain.com
 
'    $user = new 
UserManager\CMS\User('Alejandro','Gervasio','alejandrodomain.com
 
');
it throws a fatal error saying Exception class can not be found.
PHP Fatal error:  Class 'UserManager\CMS\Exception' not found in 
C:\user\user.php on line 25
I used the same example listed below with little change;
http://www.devshed.com/c/a/PHP/Using-Namespaces-in-PHP-5/3/
 
Could you let me know why namespace is not able to recognize the 
Exception class.
Reproduce code:
---------------
Filename: user.php
<?php
namespace UserManager\CMS;
class User{
	private $firstName;
	private $lastName;
	private $email;
	public function __construct($firstName,$lastName,$email){
		if(!$firstName||strlen($firstName)>32){
			throw new Exception('Invalid First Name parameter!');
		}
		if(!$lastName||strlen($lastName)>32){
			throw new Exception('Invalid Last Name parameter!');
		}
		if(!$email||!preg_match("/^.+@.+..+$/",$email)){
			throw new Exception('Invalid Email parameter!');
		}
		$this->firstName=$firstName;
		$this->lastName=$lastName;
		$this->email=$email;
	}
	// get user's first name
	public function getFirstName(){
		return $this->firstName;
	}
	// get user's last name
	public function getLastName(){
		return $this->lastName;
	}
	// get user's email
	public function getEmail(){
		return $this->email;
	}
?>
RunUser.php
<?php
include_once "user.php";
use UserManager\CMS;
try{
	// create new instance of 'User' class by using the specified namespace
	$user = new UserManager\CMS\User('Alejandro','Gervasio','alejandrodomain.com');
	// display user data
	echo 'First Name: '.$user->getFirstName().'<br />';
	echo 'Last Name: '.$user->getLastName().'<br />';
	echo 'Email: '.$user->getEmail().'<br />';
	
	$user->getValueN();
}
catch(Exception $e){
	echo $e->getMessage();
	exit();
}
?>
Expected result:
----------------
Invalid Email parameter
Actual result:
--------------
PHP Fatal error:  Class 'UserManager\CMS\Exception' not found in 
C:\user\user.php on line 25
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 06:00:01 2025 UTC | 
I got it working, I added the global namespace to make it work. Used the \operator to do so: $e = new \Exception('my error message goes here'); Thanks