|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45681 namespace visibility
Submitted: 2008-08-01 16:25 UTC Modified: 2021-07-16 15:30 UTC
Avg. Score:4.8 ± 0.4
Reproduced:14 of 14 (100.0%)
Same Version:6 (42.9%)
Same OS:2 (14.3%)
From: frase at cs dot wisc dot edu Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 5.3.0alpha1 OS: Ubuntu 8.04 Hardy
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
49 + 32 = ?
Subscribe to this entry?

 [2008-08-01 16:25 UTC] frase at cs dot wisc dot edu
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:
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";

Expected result:
Test::A can see Test::A::nsVar
Test::B can see Test::A::nsVar

Actual result:
(syntax error on 'namespace', line 4)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-01 16:33 UTC] frase at cs dot wisc dot edu
Also: This would also eliminate the need for class visibility, as mentioned in i.e. Bug #44194.  By declaring everything in a class (including the constructor) with the 'namespace' (or lesser) visibility, nothing outside the namespace could instantiate or access it, so the class itself would be effectively invisible outside the namespace.
 [2008-08-25 18:15 UTC] ganswijk at xs4all dot nl
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 {

Also see:
 [2016-12-31 00:25 UTC]
-Package: Feature/Change Request +Package: Scripting Engine problem
 [2021-07-16 15:30 UTC]
-Status: Open +Status: Suspended
 [2021-07-16 15:30 UTC]
This feature would certainly need an RFC[1].  I suspend this
ticket for the time being.

[1] <>
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Sep 28 11:03:37 2021 UTC