|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-02-29 11:06 UTC] mr at segtel dot dk
Description:
------------
I would like to request a package access control system to accompany public, private and protected as defined by OMG for UML.
Reproduce code:
---------------
package my_package {
class some_class {
package $var;
}
class other_class {
private $obj;
public function __construct() {
$this->obj = new some_class();
}
public function do_something() {
print $this->obj->var;
}
}
}
Expected result:
----------------
The above should work. my_package::some_class::var is accessible by other classes inside the my_package package whereas any other class does not have access.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 11:00:02 2025 UTC |
I would love that feature as well. I would have loved namespaces to be implemented that way, but it seems like nowdays those are most commonly used for autoloaders to find the scripts inside a folder structure (psr-4). I'd like to have "package" work similar to a namespace though, so it'll work easier across multiple files, but with visibility on classes. Example code: ------------- <?php package App\User; namespace App\User\Some\Namespace; // not available outside package "App\User" private class UserRepository { ... } <?php package App\User; namespace App\User\Some\Other\Namespace; use App\User\Some\Namespace\UserRepository; // available outside package public class UserFactory { private function getUserRepository(): UserRepository { return new UserRepository(); // works, because it's in the same package } ... } Expected result: ---------------- <?php // no package, no namespace // or different package // or just namespace without package use App\User\Some\Namespace\UserRepository; // will cause error (private class) use App\User\Some\Other\Namespace\UserFactory; // works (public class) It would allow developers to "hide" classes intended for use inside a part of a program (module, bundle or similar) only and enforce the use of design patterns like factories or facades for where it is deemed necessary. It would deny unintended uses of classes and reduce the possibility of unwanted dependencies. Also, as a minor quality of life improvement, it would allow IDE developers to reduce the amount of possible classes shown after typing "new", which can be quite a lot in big projects, although only a few are actually intended to be used alongside "new", outside the 'package' they come from.