note 119234 modified in language.namespaces.faq by cmb
| From: | cmb@php.net | Date: | Mon, 30 May 2016 15:19:03 +0000 |
| Subject: | note 119234 modified in language.namespaces.faq by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-205621@lists.php.net to get a copy of this message | ||
[Editor's note: that behavior is caused by a bug in PHP 7.0, which has been fixed as of PHP
7.0.7.]
Regarding the entry "Import names cannot conflict with classes defined in the same file".
- I found that since PHP 7.0 this is no longer the case.
In PHP 7.0 you can have a class with a name that matches an imported class (or namespace or both at
the same time).
<?php
namespace ns1 {
class ns1 {
public static function write() {
echo "ns1\\ns1::write()\n";
}
}
}
namespace ns1\ns1 {
class ns1c {
public static function write() {
echo "ns1\\ns1\\ns1c::write()\n";
}
}
}
namespace ns2 {
use ns1\ns1 as ns1; // both a class in ns1, and a namespace ns1\ns1
// the next class causes fatal error in php 5.6, not in 7.0
class ns1 {
public static function write() {
echo "ns2\\ns1::write()\n";
}
}
ns1::write(); // calls imported ns1\ns1::write()
ns1\ns1c::write(); // calls imported ns1\ns1\ns1c::write()
namespace\ns1::write(); // calls ns2\ns1::write()
}
?>
--was--
Regarding the entry "Import names cannot conflict with classes defined in the same file".
- I found that since PHP 7.0 this is no longer the case.
In PHP 7.0 you can have a class with a name that matches an imported class (or namespace or both at
the same time).
<?php
namespace ns1 {
class ns1 {
public static function write() {
echo "ns1\\ns1::write()\n";
}
}
}
namespace ns1\ns1 {
class ns1c {
public static function write() {
echo "ns1\\ns1\\ns1c::write()\n";
}
}
}
namespace ns2 {
use ns1\ns1 as ns1; // both a class in ns1, and a namespace ns1\ns1
// the next class causes fatal error in php 5.6, not in 7.0
class ns1 {
public static function write() {
echo "ns2\\ns1::write()\n";
}
}
ns1::write(); // calls imported ns1\ns1::write()
ns1\ns1c::write(); // calls imported ns1\ns1\ns1c::write()
namespace\ns1::write(); // calls ns2\ns1::write()
}
?>
http://php.net/manual/en/language.namespaces.faq.php