php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48825 Namespace throws fatal error if any exception is handled within a class
Submitted: 2009-07-06 23:28 UTC Modified: 2009-07-07 03:03 UTC
From: shiva dot buzz at gmail dot com Assigned:
Status: Closed Package: *General Issues
PHP Version: 5.3.0 OS: Windows VISTA
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: shiva dot buzz at gmail dot com
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-07 03:03 UTC] shiva dot buzz at gmail dot com
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
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Mar 14 15:01:30 2025 UTC