Skip to content

Instantly share code, notes, and snippets.

@brzuchal
Last active June 8, 2016 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brzuchal/8411a9b299f521e0186a2077be8f5321 to your computer and use it in GitHub Desktop.
Save brzuchal/8411a9b299f521e0186a2077be8f5321 to your computer and use it in GitHub Desktop.
PHP package-private class members
<?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
{
}
}
}
<?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();
}
}
}
}
<?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();
}
}
@brzuchal
Copy link
Author

brzuchal commented Jun 8, 2016

IMHO every namespace and package should cause zend_class_entry *ce->ns to be something like zend_namespace_entry ns with visibility flags this will bring quite enought optimisation caused with no string comparison rather than pointer comparison. Such zend_namespace_entry ne->parent could be parent zend_namespace_entry pointer to optimise comparison.

@brzuchal
Copy link
Author

brzuchal commented Jun 8, 2016

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.

@brzuchal
Copy link
Author

brzuchal commented Jun 8, 2016

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