# Class Access Modifiers

- Thread: https://externals.io/message/51562
- Original archive: https://news-web.php.net/php.internals/51562
- Started by Jarrod Nettles on 2011-03-03T17:21:35+00:00
- Messages: 9
- 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

---

## #51562 — Jarrod Nettles — 2011-03-03T17:21:35+00:00

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

Has there been any discussion on access modifiers for classes? I looked through the existing RFCs and searched through old discussions on the mailing list but didn't come up with anything.

Specifically, I think it would be beneficial (for framework developers in particular) if classes could be marked as public, private, etc. I haven't really thought through exact definitions on how each modifier would restrict but here is a use case.

A developer is working on an object-oriented framework that uses namespaces and uses classes extensively. He considers many of the classes to be for internal use only, that is, they will only be used by the internal workings of the framework core, not by any web application that somebody builds using his framework. That being the case, the developer would like to restrict access to certain classes so  that they can only be accessed in certain situations.

Proposal (after five minutes of thought)


1.       Public - A class can be instantiated or called statically from anywhere. For reasons of backward compatibility a class without any modifier would be considered public.

2.       Internal - A class can only be instantiated/called from within the same root namespace. If I have a class Core\Mvc\View, only from within a class sharing the same root namespace (ex: Core\Html\Textbox) would I be able to access the "View" class.

3.       Private - A class can only be instantiated/called from within the exact same namespace. Example, class Core\Mvc\View could only be accessed from a class in the Core\Mvc namespace (ex: Core\Mvc\Controller).

What do people think? I'm not too concerned with the exact three I listed above, but more and more I think it would be wise if there were a way to restrict the accessibility of classes between namespaces.

Jarrod Nettles
Application Developer - Technology
INCCRRA
p 309.829.5327 - f 309.828.1808

This e-mail message may contain privileged or confidential information. If you are not the intended recipient, you may not disclose, use, disseminate, distribute, copy or rely upon this message or attachment in any way. If you received this e-mail message in error, please return by forwarding the message and its attachments to the sender. INCCRRA does not accept liability for any errors, omissions, corruption or virus in the contents of this message or any attachments that arises as a result of e-mail transmission.

Please consider your environmental responsibility before printing this e-mail

---

## #51563 — Martin Scotta — 2011-03-03T18:09:21+00:00

URL: https://externals.io/message/51562#51563 · In reply to: #51562

I'm writing a RFC about improvements on the current OO Model.

do you want to add this?

http://wiki.php.net/rfc/object-model-improvements

 Martin Scotta




> Has there been any discussion on access modifiers for classes? I looked
> through the existing RFCs and searched through old discussions on the
> mailing list but didn't come up with anything.
>
> Specifically, I think it would be beneficial (for framework developers in
> particular) if classes could be marked as public, private, etc. I haven't
> really thought through exact definitions on how each modifier would restrict
> but here is a use case.
>
> A developer is working on an object-oriented framework that uses namespaces
> and uses classes extensively. He considers many of the classes to be for
> internal use only, that is, they will only be used by the internal workings
> of the framework core, not by any web application that somebody builds using
> his framework. That being the case, the developer would like to restrict
> access to certain classes so  that they can only be accessed in certain
> situations.
>
> Proposal (after five minutes of thought)
>
>
> 1.       Public - A class can be instantiated or called statically from
> anywhere. For reasons of backward compatibility a class without any modifier
> would be considered public.
>
> 2.       Internal - A class can only be instantiated/called from within the
> same root namespace. If I have a class Core\Mvc\View, only from within a
> class sharing the same root namespace (ex: Core\Html\Textbox) would I be
> able to access the "View" class.
>
> 3.       Private - A class can only be instantiated/called from within the
> exact same namespace. Example, class Core\Mvc\View could only be accessed
> from a class in the Core\Mvc namespace (ex: Core\Mvc\Controller).
>
> What do people think? I'm not too concerned with the exact three I listed
> above, but more and more I think it would be wise if there were a way to
> restrict the accessibility of classes between namespaces.
>
> Jarrod Nettles
> Application Developer - Technology
> INCCRRA
> p 309.829.5327 - f 309.828.1808
>
> This e-mail message may contain privileged or confidential information. If
> you are not the intended recipient, you may not disclose, use, disseminate,
> distribute, copy or rely upon this message or attachment in any way. If you
> received this e-mail message in error, please return by forwarding the
> message and its attachments to the sender. INCCRRA does not accept liability
> for any errors, omissions, corruption or virus in the contents of this
> message or any attachments that arises as a result of e-mail transmission.
>
> Please consider your environmental responsibility before printing this
> e-mail

---

## #51628 — Jarrod Nettles — 2011-03-08T21:27:48+00:00

URL: https://externals.io/message/51562#51628 · In reply to: #51563

Either that or it should be spun off into its own RFC. I have no idea how to do either.

From: Martin Scotta [mailto:martinscotta@gmail.com]
Sent: Thursday, March 03, 2011 12:09 PM
To: Jarrod Nettles
Cc: internals@lists.php.net
Subject: Re: [PHP-DEV] Class Access Modifiers

I'm writing a RFC about improvements on the current OO Model.

do you want to add this?

http://wiki.php.net/rfc/object-model-improvements

 Martin Scotta


Has there been any discussion on access modifiers for classes? I looked through the existing RFCs and searched through old discussions on the mailing list but didn't come up with anything.

Specifically, I think it would be beneficial (for framework developers in particular) if classes could be marked as public, private, etc. I haven't really thought through exact definitions on how each modifier would restrict but here is a use case.

A developer is working on an object-oriented framework that uses namespaces and uses classes extensively. He considers many of the classes to be for internal use only, that is, they will only be used by the internal workings of the framework core, not by any web application that somebody builds using his framework. That being the case, the developer would like to restrict access to certain classes so  that they can only be accessed in certain situations.

Proposal (after five minutes of thought)


1.       Public - A class can be instantiated or called statically from anywhere. For reasons of backward compatibility a class without any modifier would be considered public.

2.       Internal - A class can only be instantiated/called from within the same root namespace. If I have a class Core\Mvc\View, only from within a class sharing the same root namespace (ex: Core\Html\Textbox) would I be able to access the "View" class.

3.       Private - A class can only be instantiated/called from within the exact same namespace. Example, class Core\Mvc\View could only be accessed from a class in the Core\Mvc namespace (ex: Core\Mvc\Controller).

What do people think? I'm not too concerned with the exact three I listed above, but more and more I think it would be wise if there were a way to restrict the accessibility of classes between namespaces.

Jarrod Nettles
Application Developer - Technology
INCCRRA
p 309.829.5327 - f 309.828.1808

This e-mail message may contain privileged or confidential information. If you are not the intended recipient, you may not disclose, use, disseminate, distribute, copy or rely upon this message or attachment in any way. If you received this e-mail message in error, please return by forwarding the message and its attachments to the sender. INCCRRA does not accept liability for any errors, omissions, corruption or virus in the contents of this message or any attachments that arises as a result of e-mail transmission.

Please consider your environmental responsibility before printing this e-mail


Jarrod Nettles
Application Developer - Technology
INCCRRA
p 309.829.5327 - f 309.828.1808

This e-mail message may contain privileged or confidential information. If you are not the intended recipient, you may not disclose, use, disseminate, distribute, copy or rely upon this message or attachment in any way. If you received this e-mail message in error, please return by forwarding the message and its attachments to the sender. INCCRRA does not accept liability for any errors, omissions, corruption or virus in the contents of this message or any attachments that arises as a result of e-mail transmission.

Please consider your environmental responsibility before printing this e-mail

---

## #51639 — Hannes Landeholm — 2011-03-09T15:11:07+00:00

URL: https://externals.io/message/51562#51639 · In reply to: #51562

Good Sugesstion.

Currently I'm forced to use huge internal classes for my framework because
dividing them into smaller classes would expose internal behavior to the
"outside"... the application layer.

This would solve that problem.

~Hannes




> Has there been any discussion on access modifiers for classes? I looked
> through the existing RFCs and searched through old discussions on the
> mailing list but didn't come up with anything.
>
> Specifically, I think it would be beneficial (for framework developers in
> particular) if classes could be marked as public, private, etc. I haven't
> really thought through exact definitions on how each modifier would restrict
> but here is a use case.
>
> A developer is working on an object-oriented framework that uses namespaces
> and uses classes extensively. He considers many of the classes to be for
> internal use only, that is, they will only be used by the internal workings
> of the framework core, not by any web application that somebody builds using
> his framework. That being the case, the developer would like to restrict
> access to certain classes so  that they can only be accessed in certain
> situations.
>
> Proposal (after five minutes of thought)
>
>
> 1.       Public - A class can be instantiated or called statically from
> anywhere. For reasons of backward compatibility a class without any modifier
> would be considered public.
>
> 2.       Internal - A class can only be instantiated/called from within the
> same root namespace. If I have a class Core\Mvc\View, only from within a
> class sharing the same root namespace (ex: Core\Html\Textbox) would I be
> able to access the "View" class.
>
> 3.       Private - A class can only be instantiated/called from within the
> exact same namespace. Example, class Core\Mvc\View could only be accessed
> from a class in the Core\Mvc namespace (ex: Core\Mvc\Controller).
>
> What do people think? I'm not too concerned with the exact three I listed
> above, but more and more I think it would be wise if there were a way to
> restrict the accessibility of classes between namespaces.
>
> Jarrod Nettles
> Application Developer - Technology
> INCCRRA
> p 309.829.5327 - f 309.828.1808
>
> This e-mail message may contain privileged or confidential information. If
> you are not the intended recipient, you may not disclose, use, disseminate,
> distribute, copy or rely upon this message or attachment in any way. If you
> received this e-mail message in error, please return by forwarding the
> message and its attachments to the sender. INCCRRA does not accept liability
> for any errors, omissions, corruption or virus in the contents of this
> message or any attachments that arises as a result of e-mail transmission.
>
> Please consider your environmental responsibility before printing this
> e-mail

---

## #51641 — James Butler — 2011-03-09T15:18:39+00:00

URL: https://externals.io/message/51562#51641 · In reply to: #51639

> > […]
Really like the general idea and think (IMHO) its RFC worthy, but have a
few questions...
Would there be any method of overriding the the visibility in a child
class?And would the child class inherit the parents visibility?
I realise this may seem daft initially but I can think of some instances
where this might be useful.

James

---

## #51646 — Jarrod Nettles — 2011-03-09T18:02:36+00:00

URL: https://externals.io/message/51562#51646 · In reply to: #51639

Interesting question. My gut tells me not (as does three years of C# experience). I’m sure that everyone will have a different opinion on this but to me it seems taboo that a child class override the visibility of the parent class. For example, PHP currently does not allow you to override a method with a lower or higher visibility than the parent – I can’t go from protected to public or vice versa. Visibility must be maintained throughout the class hierarchy.

I realize that ten thousand people will curse up and down the first time they try  to extend an internal class with a public one but good application design should be the real solution.


> > […]
Really like the general idea and think (IMHO) its RFC worthy, but have a
few questions...
Would there be any method of overriding the the visibility in a child
class?And would the child class inherit the parents visibility?
I realise this may seem daft initially but I can think of some instances
where this might be useful.

James

Jarrod Nettles
Application Developer - Technology
INCCRRA
p 309.829.5327 - f 309.828.1808

This e-mail message may contain privileged or confidential information. If you are not the intended recipient, you may not disclose, use, disseminate, distribute, copy or rely upon this message or attachment in any way. If you received this e-mail message in error, please return by forwarding the message and its attachments to the sender. INCCRRA does not accept liability for any errors, omissions, corruption or virus in the contents of this message or any attachments that arises as a result of e-mail transmission.

Please consider your environmental responsibility before printing this e-mail

---

## #51647 — Chad Fulton — 2011-03-09T18:23:16+00:00

URL: https://externals.io/message/51562#51647 · In reply to: #51646

Hello,


> Interesting question. My gut tells me not (as does three years of C# experience). I’m sure that everyone will have a different opinion on this but to me it seems taboo that a child class override the visibility of the parent class. For example, PHP currently does not allow you to override a method with a lower or higher visibility than the parent – I can’t go from protected to public or vice versa. Visibility must be maintained throughout the class hierarchy.
>

Actually, class properties and methods can have a higher visibility
than their parents, just not a lower one.

E.g.:

<?php

error_reporting(E_ALL | E_STRICT);

class foo {
    protected function bar() {

    }
}

class baz extends foo {
    public function bar() {

    }
}

class foobar extends baz {
    protected function bar () {

    }
}

?>

You get the following error:

Fatal error: Access level to foobar::bar() must be public (as in class
baz) in ...

-------

That said, I wouldn't think that visibility modifiers on classes need
to follow the same pattern. In the case of properties and methods, I
think the rationale is that child classes should be compatible from an
interface standpoint with their parents. That same logic may not
transfer to class visibility modifiers.

I am certainly no expert, but I'm curious what the use case is for
class visibility modifiers?


> Currently I'm forced to use huge internal classes for my framework because
dividing them into smaller classes would expose internal behavior to the
"outside"... the application layer.
>

This doesn't necessarily make sense to me. Isn't it the
end-developer's problem if they start instantiating classes in weird
ways?

It doesn't seem the same as having a private method, since in that
case the end-developer presumably already has an object instance, and
you want to guide them to the correct interface for interacting with
it, whereas in an application, one would think that the end-developer
wouldn't simply be instantiating classes by themselves all over the
place.

Chad

---

## #51650 — Martin Scotta — 2011-03-09T19:33:54+00:00

URL: https://externals.io/message/51562#51650 · In reply to: #51647

class modifiers should follow the same modifiers and semantic that methods:
T_PUBLIC, `T_PROTECTED` and `T_PRIVATE` should be used.

nowadays all classes are public...

T_PUBLIC: everybody can see it, everybody can subclass it
T_PROTECTED: visible only within its ns, but everybody can subclass it
T_PRIVATE: visible only within the same ns, subclass allowed only within its
ns

namespace A {
    class Pub { }
    protected class Prot { }
    private class Priv { }
}

namespace B {
    class Foo extends \A\Prot { }  // legal, no matter Foo visibility
    class Baz extends \A\Priv { }  // error, no matter Baz visibility
    new Foo();  // ok
    new Baz();  // ok
    new \A\Pub();  // ok
    new \A\Prot(); // error, class is protected
    new \A\Priv(); // error, class is private
}

in the example "A" exposes classes "Pub" and "Prot", but you cannot
instantiate class "Prot"

So... does it mean that class "Priv" cannot be used outside "A"? no, you can
always return a reference.

namespace A {
   class F {
    static function getE() { return new E; }
   }
   private class E { }
}

var_dump( \A\F::getE() ); // object \A\E


 Martin Scotta




> Hello,
>
> On Wed, Mar 9, 2011 at 10:02 AM, Jarrod Nettles <JNettles@inccrra.org>
> wrote:
> > […]
> experience). I’m sure that everyone will have a different opinion on this
> but to me it seems taboo that a child class override the visibility of the
> parent class. For example, PHP currently does not allow you to override a
> method with a lower or higher visibility than the parent – I can’t go from
> protected to public or vice versa. Visibility must be maintained throughout
> the class hierarchy.
> > […]
>
> Actually, class properties and methods can have a higher visibility
> than their parents, just not a lower one.
>
> E.g.:
>
> <?php
>
> error_reporting(E_ALL | E_STRICT);
>
> class foo {
>    protected function bar() {
>
>    }
> }
>
> class baz extends foo {
>    public function bar() {
>
>    }
> }
>
> class foobar extends baz {
>    protected function bar () {
>
>    }
> }
>
> ?>
>
> You get the following error:
>
> Fatal error: Access level to foobar::bar() must be public (as in class
> baz) in ...
>
> -------
>
> That said, I wouldn't think that visibility modifiers on classes need
> to follow the same pattern. In the case of properties and methods, I
> think the rationale is that child classes should be compatible from an
> interface standpoint with their parents. That same logic may not
> transfer to class visibility modifiers.
>
> I am certainly no expert, but I'm curious what the use case is for
> class visibility modifiers?
>
> On Wed, Mar 9, 2011 at 7:11 AM, Hannes Landeholm <landeholm@gmail.com>
> wrote:
> > […]
> because
> dividing them into smaller classes would expose internal behavior to the
> "outside"... the application layer.
> > […]
>
> This doesn't necessarily make sense to me. Isn't it the
> end-developer's problem if they start instantiating classes in weird
> ways?
>
> It doesn't seem the same as having a private method, since in that
> case the end-developer presumably already has an object instance, and
> you want to guide them to the correct interface for interacting with
> it, whereas in an application, one would think that the end-developer
> wouldn't simply be instantiating classes by themselves all over the
> place.
>
> Chad

---

## #51652 — Martin Scotta — 2011-03-09T19:43:01+00:00

URL: https://externals.io/message/51562#51652 · In reply to: #51647

> Hello,
>
> On Wed, Mar 9, 2011 at 10:02 AM, Jarrod Nettles <JNettles@inccrra.org>
> wrote:
> > […]
> experience). I’m sure that everyone will have a different opinion on this
> but to me it seems taboo that a child class override the visibility of the
> parent class. For example, PHP currently does not allow you to override a
> method with a lower or higher visibility than the parent – I can’t go from
> protected to public or vice versa. Visibility must be maintained throughout
> the class hierarchy.
> > […]
>
> Actually, class properties and methods can have a higher visibility
> than their parents, just not a lower one.
>
> E.g.:
>
> <?php
>
> error_reporting(E_ALL | E_STRICT);
>
> class foo {
>    protected function bar() {
>
>    }
> }
>
> class baz extends foo {
>    public function bar() {
>
>    }
> }
>
> class foobar extends baz {
>    protected function bar () {
>
>    }
> }
>
> ?>
>
> You get the following error:
>
> Fatal error: Access level to foobar::bar() must be public (as in class
> baz) in ...
>
> -------
>
> That said, I wouldn't think that visibility modifiers on classes need
> to follow the same pattern. In the case of properties and methods, I
> think the rationale is that child classes should be compatible from an
> interface standpoint with their parents. That same logic may not
> transfer to class visibility modifiers.
>

yes, every class should specify its own visibility.
Inheritance here does not make any sense, you only will be able to use
private classes at the cost of your entire class hierarchy being private too
.




>
> I am certainly no expert, but I'm curious what the use case is for
> class visibility modifiers?
>
> On Wed, Mar 9, 2011 at 7:11 AM, Hannes Landeholm <landeholm@gmail.com>
> wrote:
> > […]
> because
> dividing them into smaller classes would expose internal behavior to the
> "outside"... the application layer.
> > […]
>
> This doesn't necessarily make sense to me. Isn't it the
> end-developer's problem if they start instantiating classes in weird
> ways?
>

Well, that depends on who is your end-user?
Current object model works well for small-to-mid size application, but it
does not allows to write *safe* libraries

safe: to don't break client code, because of wrong client code.


> It doesn't seem the same as having a private method, since in that
> case the end-developer presumably already has an object instance, and
> you want to guide them to the correct interface for interacting with
> it, whereas in an application, one would think that the end-developer
> wouldn't simply be instantiating classes by themselves all over the
> place.
>
> Chad
