php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70671 Fatal Error: Cannot use class because the name is already in use in
Submitted: 2015-10-08 15:26 UTC Modified: 2016-06-24 19:17 UTC
Votes:7
Avg. Score:4.6 ± 0.5
Reproduced:6 of 6 (100.0%)
Same Version:6 (100.0%)
Same OS:3 (50.0%)
From: david at grudl dot com Assigned: cmb (profile)
Status: Duplicate Package: Class/Object related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2015-10-08 15:26 UTC] david at grudl dot com
Description:
------------
PHP produces unnecessary "Fatal error: Cannot use Something\Foo as Foo because the name is already in use in bar.php". There is no reason to throw such error.

The error is insidious because it dependents on the order in which the files are loaded, which is (with autoloading) unpredictable. In reverse order (see demo-ok.php) is everything ok.

Moreover, in PHP 7 it is not possible to catch this error via try ... catch or even via set_exception_handler(). 



Test script:
---------------
foo.php:

<?php
namespace A;

class Foo 
{}
?>

------------
bar.php:

<?php
namespace A;

use Something\Foo;
?>


-------------
demo-fatalerror.php

<?php
include 'foo.php';
include 'bar.php';
?>


-------------
demo-ok.php

<?php
include 'bar.php';
include 'foo.php';
?>





Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-24 19:17 UTC] cmb@php.net
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2016-06-24 19:17 UTC] cmb@php.net
Duplicate of bug #66862.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Sep 15 12:01:26 2019 UTC