Last active
June 8, 2016 16:28
-
-
Save brzuchal/8411a9b299f521e0186a2077be8f5321 to your computer and use it in GitHub Desktop.
PHP package-private class members
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
package MyVendor\MyLibrary // acts same as `namespace` but changes all classes into package-private | |
{ | |
namespace MyInternalStuff // cause current namespace would be MyVendor\MyLibrary\MyInternalStuff | |
{ | |
class MyNonPublicWidget // class name would be MyVendor\MyLibrary\MyInternalStuff\MyNonPublicWidget | |
{ | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
package MyVendor\MyLibrary | |
{ | |
namespace // missing name cause class ns woul be MyVendor\MyLibrary | |
{ | |
public class PublicApi // any package class need to be public prefixed for visibility in lower level namespaces (out of package namespace) | |
{ | |
private $widget; | |
public function __construct() | |
{ | |
$this->widget = new MyInternalStuff\MyNonPublicWidget(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
package MyVendor\MyLibrary; // acts same as `namespace` but changes all classes into package-private | |
public class PublicApi // as of we are in package we need to set public visibility in packag lower namespaces | |
{ | |
private $widget; | |
public function __construct() | |
{ | |
$this->widget = new MyInternalStuff\MyNonPublicWidget(); | |
} | |
} |
Syntactically package
keyword can be used once, there is no possibility for such declaration nesting because it would cause another problem, such: does package in package public class should be visible for upper package namespace only or global namespace either?
It would lead to more complex problems.
The namespace
keyword if not providing any name inside package
declaration is optional, so provided with missing name has no effect.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IMHO every
namespace
andpackage
should causezend_class_entry *ce->ns
to be something likezend_namespace_entry ns
with visibility flags this will bring quite enought optimisation caused with no string comparison rather than pointer comparison. Suchzend_namespace_entry ne->parent
could be parentzend_namespace_entry
pointer to optimise comparison.