rfc:namespace_scoped_declares

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:namespace_scoped_declares [2016/09/24 12:21] nikicrfc:namespace_scoped_declares [2017/12/10 22:03] – Resolve one open question nikic
Line 3: Line 3:
   * Date: 2016-09-20   * Date: 2016-09-20
   * Author: Nikita Popov <nikic@php.net>   * Author: Nikita Popov <nikic@php.net>
-  * Proposed PHP version: PHP 7.2+  * Proposed PHP version: PHP 7.3
   * Status: Under Discussion   * Status: Under Discussion
 +  * PR: https://github.com/php/php-src/pull/2972
   * ML thread: http://externals.io/thread/326   * ML thread: http://externals.io/thread/326
  
Line 73: Line 74:
 The given ''$directives'' will be used as default values for all code inside the namespace ''$namespace'' or a sub-namespace. A sub-namespace is any namespace that starts with ''$namespace . "\\"''. (Note that namespaces are case-insensitive, so ''namespace_declare'' also applies case-insensitively.) The given ''$directives'' will be used as default values for all code inside the namespace ''$namespace'' or a sub-namespace. A sub-namespace is any namespace that starts with ''$namespace . "\\"''. (Note that namespaces are case-insensitive, so ''namespace_declare'' also applies case-insensitively.)
  
-If the provided ''$namespace'' is the empty string, an ''Error'' is thrown. It is explicitly not possible to specify a global default.+If the provided ''$namespace'' is the empty string, an ''Error'' is thrown. It is explicitly not possible to specify a global default. If ''$namespace'' starts or ends with a backslash, an ''Error'' is thrown.
  
 If ''namespace_declare'' was already called for a certain ''$namespace'', another call with the same namespace will result in an ''Error''. If ''namespace_declare'' was already called for a certain ''$namespace'', another call with the same namespace will result in an ''Error''.
  
-The declare directive defaults specified by ''namespace_declare()'' take effect for all files compiled (i.e. included**after** the execution of the function call. Files included prior to the function call are not affectedThe file that contains the call is also not affectedbecause the function will only be executed after the file has already been compiled.+If code using ''$namespace'' or a sub-namespace has already been used (compiled or loaded), an ''Error'' is thrownThis avoids situations where code in a namespace is loaded, subsequently ''namespace_declare()'' is used and more code from the namespace is loadedWithout this checkcode in the same namespace would end up using inconsistent declare directives.
  
 ''namespace_declare'' has no impact on aliases created using ''class_alias'' and entities declared by internal code. ''namespace_declare'' has no impact on aliases created using ''class_alias'' and entities declared by internal code.
Line 130: Line 131:
  
 If we go for throwing a warning here, it might be beneficial to add a ''supports_declare()'' function, which allows you to determine whether a certain declare directive is supported, without resorting to a PHP version check (which would likely be unreliable with regard to alternative PHP implementations). If we go for throwing a warning here, it might be beneficial to add a ''supports_declare()'' function, which allows you to determine whether a certain declare directive is supported, without resorting to a PHP version check (which would likely be unreliable with regard to alternative PHP implementations).
- 
-==== Open Question: namespace_declare after namespace already used ==== 
- 
-As specified above, it is possible to first load code in a certain namespace and subsequently call ''namespace_declare()''. In this case the already compiled code will not be affected by the ''namespace_declare()'', which is bound to cause confusion. 
- 
-To avoid this, ''namespace_declare()'' could throw an ''Error'' if it called after code in the given namespace (or a sub-namespace) has already been loaded. This would require us to internally track which namespaces have had code loaded, something which we currently don't do. 
  
 ===== Challenges, Disadvantages, Alternatives ===== ===== Challenges, Disadvantages, Alternatives =====
Line 190: Line 185:
  
   * Resolve open question "Handling of unknown directives"   * Resolve open question "Handling of unknown directives"
-  * Resolve open question "namespace_declare after namespace already used" 
   * Consider adding some introspection functionality, such as ''get_namespace_declares()''   * Consider adding some introspection functionality, such as ''get_namespace_declares()''
   * Investigate whether this mechanism can support the ''encoding'' directive, which is a lot more magic than the rest.   * Investigate whether this mechanism can support the ''encoding'' directive, which is a lot more magic than the rest.
-  * Actually implement this thing. 
rfc/namespace_scoped_declares.txt · Last modified: 2022/01/25 18:22 by ilutov