rfc:private-classes-and-functions

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
rfc:private-classes-and-functions [2024/12/11 15:09] ilutovrfc:private-classes-and-functions [2024/12/11 15:14] (current) ilutov
Line 33: Line 33:
 ===== Rationale ===== ===== Rationale =====
  
-As foreshadowed, one of the primary advantages of marking classes and functions of <php>private</php> is that they are removed from the public API, and prevent users from interacting with them in ways that are not intended by the library author. Today, such structures are commonly marked with a <php>/** @internal */</php> comment. Native support for <php>private</php> would provide stronger guarantees that the structure is not used outside of its intended scope.+As foreshadowed, one of the primary advantages of marking classes and functions of <php>private</php> is that they are removed from the public API, and prevent users from interacting with them in ways that are not intended by the library author. Today, such structures are commonly marked with a <php>/** @internal */</php> comment. Native support for <php>private</php> would provide stronger guarantees that they are not used outside of their intended scope.
  
 The other primary motivator is to avoid clashes for classes used within single files. A good use-case for these are temporary classes in tests. The other primary motivator is to avoid clashes for classes used within single files. A good use-case for these are temporary classes in tests.
Line 53: Line 53:
 We don't have to worry about disambiguation of the <php>DbMock</php> class name, as it is only used in the current file and thus cannot clash with other private (or even public, non-<php>use</php>d) classes. We don't have to worry about disambiguation of the <php>DbMock</php> class name, as it is only used in the current file and thus cannot clash with other private (or even public, non-<php>use</php>d) classes.
  
-Somewhat less importantly: In PHP, classes are usually placed in separate files to play nice with autoloaders. Classes that are exclusively used within a single other file could be moved to said file without issues. However, this puts the class in a weird state where it may still be used within other files, but can't be autoloaded on its own. <php>private</php> classes will necessarily be placed in the file they are usedand as they aren'accessible in other files, autoloading becomes irrelevant.+Somewhat less importantly: In PHP, classes are usually placed in separate files to play nice with autoloaders. Classes that are exclusively used within a single other file could be moved to said file without issues. However, this puts the class in a weird state where it may still be used within other files, but can't be autoloaded on its own. <php>private</php> classes will necessarily be placed in the file from which they are usedand given that they are not accessible in from files, autoloading becomes irrelevant.
  
 ===== Semantics ===== ===== Semantics =====
  
-<php>private</php> for classes and functions works similar to anonymous classes. Namely, the name of the structure is mangled by appending the file name, line number and incrementing id to the original name. Any usage of this name inside the current file is replaced with the mangled name through the same mechanism used by <php>use [function] Foo as Bar;</php>. This avoids trivial usage of the structure outside of its intended scope, and avoids accidental naming conflicts for internal structures.+<php>private</php> for classes and functions works similar to anonymous classes. Namely, the name of the structure is mangled by appending the file name, line number and incrementing id to the original name. Any usage of this name inside the current file is replaced with the mangled name at compile time, through the same mechanism used by <php>use [function] Foo as Bar;</php>. This avoids trivial usage of the structure outside of its intended scope, and avoids accidental naming conflicts for internal structures.
  
 <code php> <code php>
rfc/private-classes-and-functions.1733929797.txt.gz · Last modified: 2024/12/11 15:09 by ilutov