|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-08-01 16:25 UTC] frase at cs dot wisc dot edu
Description:
------------
When PHP first introduced visibility, it did not yet have namespaces, so the only applicable visibility schemes were related to classes -- members were either global (public), or local to the class (private), possibly including parents and children (protected).
But with the addition of namespaces, there is now a need for a visibility scope between public and protected. Developers (like me) will use namespaces to create packages -- collections of classes that are meant to work closely with each other. In this context, it would be very convenient to be able to make members visible to other (unrelated-by-inheritance) classes within the same package (namespace), but hidden from the world outside the package.
Since the 'namespace' keyword is currently only allowed on the first line of a file, and therefore could not occur inside a class, I suggest re-using it for this purpose rather than introducing another reserved word.
Reproduce code:
---------------
<?php
namespace Test;
class A {
namespace static $nsVar = 'Test::A::nsVar';
static function test() {
echo __CLASS__." can see ".A::$nsVar."\n";
}
}
class B {
static function test() {
echo __CLASS__." can see ".A::$nsVar."\n";
}
}
Test::A::test();
Test::B::test();
Expected result:
----------------
Test::A can see Test::A::nsVar
Test::B can see Test::A::nsVar
Actual result:
--------------
(syntax error on 'namespace', line 4)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 22:00:02 2025 UTC |
What strange that it can only be used on the first line. That sounds very Fortran/Cobol-like! I am not very familiar with namespaces yet, but I happened to study some C# code lately and they seem to have a much better solution: namespace name-of-namespace { code-in-the-name-space } Also see: http://msdn.microsoft.com/en-us/library/z2kcy19k(VS.80).aspx