On 18/05/2024 15:00, Robert Landers wrote:
I've been thinking about having an "internal" attribute that will emit
a warning if called from outside it's left-most namespace.


I like the general idea, but I don't think limiting to "left-most namespace" is the best semantics.

It's very common for the top-level namespace to represent a vendor, and the *second* level to be the specific package, e.g. Doctrine\DBAL vs Doctrine\ORM. You've even used that in your example - I presume you've made a typo, and meant both examples to be calling PackageA not PackageB. In other cases, there are more levels - e.g. Composer package "doctrine/mongodb-odm" has root namespace "Doctrine\ODM\MongoDB".

Possibly the attribute would need some argument to specify its granularity, e.g.  #[Internal('\MyCompany\PackageA')], #[Internal('\Doctrine\ODM\MongoDB')], but that would be annoying to write each time.

This is another case where PHP suffers from its lack of a separate concept of "package" or "module" to scope things to.


My second concern is how to implement this efficiently. The check can't happen at compile-time, because we don't know the definition of SomeOtherNamespace\Foo; so the check would need to be at run-time when the method/function is called. But at run-time, namespaces have very little existence - they really are just part of the names of functions, classes, and constants.

So when calling a marked function, we would have to look up the name of the calling function or the class name of the calling method, and then do a string comparison against the namespace constraint. Maybe that would be easy and fast, I don't know.

Regards,

--
Rowan Tommins
[IMSoP]

Reply via email to