# Namespace access modifiers

- Thread: https://externals.io/message/33981
- Original archive: https://news-web.php.net/php.internals/33981
- Started by Ralph Schindler on 2007-12-12T23:19:12+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #33981 — Ralph Schindler — 2007-12-12T23:19:12+00:00

URL: https://externals.io/message/33981#33981

Since everyone is in the "namespace" frame of mind, is there a 
possibility of a "namespace" access modifier?

The way I envision this working is that if for example:

namespace Foo;

class Bar {
   nsprotected $_someProperty = null;
}

(or some other equivalent grammer)

Would allow the property Foo::Bar::$_someProperty to be accessed from 
any class that has implemented a class from the Foo namespace.

Reaching for straws or is this a possibility?

-ralph

---

## #33982 — Nathan Rixham — 2007-12-13T00:54:13+00:00

URL: https://externals.io/message/33981#33982 · In reply to: #33981

Would this not do the same thing and perhaps be easier to implement:

namespace Foo;
protected $var;

class Bar {

}

thus allowing use of private public protected before variables, and hell 
why not classes aswell.

private = only methods, code, classes in namespace Foo have access
protected = only methods, code, classes in namespace Foo or children 
Foo::Too have access
public = everybody

the same inheritance rules etc as classes.. makes sence :) and great idea!

got my vote, especially with the braces in namespaces!

namespace Foo; {
   protected $var;

   public class Extender {

      public function __construct() {
          ......
      }
   }
}

Nathan

Ralph Schindler wrote:
> Since everyone is in the "namespace" frame of mind, is there a 
> possibility of a "namespace" access modifier?
> 
> The way I envision this working is that if for example:
> 
> namespace Foo;
> 
> class Bar {
>   nsprotected $_someProperty = null;
> }
> 
> (or some other equivalent grammer)
> 
> Would allow the property Foo::Bar::$_someProperty to be accessed from 
> any class that has implemented a class from the Foo namespace.
> 
> Reaching for straws or is this a possibility?
> 
> -ralph

---

## #33983 — Jeff Moore — 2007-12-13T01:20:11+00:00

URL: https://externals.io/message/33981#33983 · In reply to: #33981

Hello Ralph,

I don't think properties are the appropriate level to specify  
namespace based access modifiers.  I do think that a coarse-grained  
access restriction on a class by class basis might be.

public class Foo {} // Default
private class Foo {}
(protected is meaningless because there is no inheritance for  
namespaces.)

In principle, I like the idea of adding an access modifier for classes  
to distinguish between a namespace's public interface and its private  
features.

But, when do you check for access violations of class names?  On use?   
On new?  What about esoteric things like reflection and the callback  
pseudo-type?  I just don't see how it could work in practice.

Best Regards,

Jeff



> Since everyone is in the "namespace" frame of mind, is there a  
> possibility of a "namespace" access modifier?
>
> The way I envision this working is that if for example:
>
> namespace Foo;
>
> class Bar {
>  nsprotected $_someProperty = null;
> }
>
> (or some other equivalent grammer)
>
> Would allow the property Foo::Bar::$_someProperty to be accessed  
> from any class that has implemented a class from the Foo namespace.
>
> Reaching for straws or is this a possibility?
>
> -ralph

---

## #33984 — Ralph Schindler — 2007-12-13T04:40:31+00:00

URL: https://externals.io/message/33981#33984 · In reply to: #33983

Jeff Moore wrote:
> I don't think properties are the appropriate level to specify namespace 
> based access modifiers.  I do think that a coarse-grained access 
> restriction on a class by class basis might be.

Perhaps you are right, but I was using that as an example.  I actually 
see great value in being able to declare a specific method inside a 
calls of a component/namespace as namespace-protected.  Alot of times 
you see that objects within a given component might do something like:

class Foo_Bar
{
     protected $_someProperty = null;
     public function _setSomeProperty($value)
     {
         $this->_someProperty = $value
     }
}

as a means to describe that an object of type Foo_Baz can interact with 
Foo_Bar, but Foo_Bar::_setSomeProperty() should not be considered a safe 
method to be called by a developer from an API standpoint.

Wouldn't that be nice?

> pseudo-type?  I just don't see how it could work in practice.

Wouldn't it work the same way as protected methods work?  Essentially, 
protected methods ensure that the caller is in the inheritance (is_a) 
chain.  This would simple add a new check (for methods marked 
nsprotected) to check to ensure that the caller is a derivative of a 
specific namespace.

-ralph
