Am 11.12.2007 um 23:36 schrieb Stanislav Malyshev:
import Name::Space;
// only stuff from Name::Space available, no PHP internal stuff
import __php__;
// now PHP's classes are loaded, too.
So "import php" make internal classes always take priority even when
there's a class in this namespace by that name? But you could
achiever the same just by avoiding naming classes the same as
internal classes, you surely know which classes are in your own
namespace? Since in both cases code change is needed, why that one
is better? If you created Name::Space::DateTime, surely there was
some intent in having it named specifically the same as DateTime
class in PHP?
Ah. Now that you mention it, there was a flaw in my logic. The idea
was not that PHP classes take precedence, but in this case, PHP's
DateTime class would already be "loaded" so no autoload would be
attempted for a Name::Space::DateTime. If, however, that
Name::Space::DateTime was already autoloaded by the time we reach that
code, things would just break. Hmmh. Jochem pointed it out in the
other message; I think this autoloading thing is going to be tricky...
Java package system is very different. For starters, they don't have
global space at all. And they have offline compilation stage which
verifies all the dependencies.
Yes, sure. I was just saying that other languages also require you to
import language-internal stuff from time to time.
I don't think that PHP's internal stuff needs to be magically
available. We can force developers to explicitly request access to
it.
We can, but we should not. Internal classes are internal for a
reason - people use them a lot. Making it hard to access frequently
used classes to enable rarely used functionality is not the right
way to go.
Okay, but what do we do about the issue described initially then?
Here's a concrete example to show the problem:
<Name/Space/DateTime.php>
namespace Name::Space;
class DateTime {}
<Name/Space/Ship.php>
namespace Name::Space;
class Ship {
public static function __autoload($className) {
// ... autoloader, can load Name::Space::DateTime
}
}
<somestuff.php>
function someStuffWeDoNotHaveControlOver() {
$foo = new Name::Space::DateTime();
// ...
}
<setup.php>
require_once('Name/Space/Ship.php');
spl_autoload_register(array('Name::Space::Ship', '__autoload'));
<example.php>
require_once('setup.php');
import Name::Space;
$d = new DateTime(); // a PHP DateTime
someStuffWeDoNotHaveControlOver();
$d = new DateTime(); // a Name::Space::DateTime now!
See what I mean?
David
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php