Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
5e7319c
initial commit
withinboredom Aug 1, 2024
3be33e9
change format to ptxt
withinboredom Aug 1, 2024
bd4cf96
apply some off-line feedback
withinboredom Aug 1, 2024
c69bff3
apply some off-line feedback
withinboredom Aug 1, 2024
49be882
Update drafts/records.md
withinboredom Aug 1, 2024
090855f
Automated changes from GitHub Actions
github-actions[bot] Aug 1, 2024
c91e18c
be more assertive
withinboredom Aug 1, 2024
7b1e32b
fix lint
withinboredom Aug 1, 2024
be7ae31
address automated feedback
withinboredom Aug 1, 2024
e9a1cc1
clarify
withinboredom Aug 1, 2024
97057b4
serialization and deserialization
withinboredom Aug 1, 2024
fc83031
clarity
withinboredom Aug 1, 2024
62c8edd
fix serialization and deserialization
withinboredom Aug 1, 2024
a490ba0
apply some feedback
withinboredom Aug 1, 2024
eb4f020
experiment with instructions
withinboredom Aug 1, 2024
9089f44
make parsable instructions since yaml parser is broke
withinboredom Aug 1, 2024
6b32127
add more details to example
withinboredom Aug 1, 2024
f7c3a05
address feedback and add more details
withinboredom Aug 2, 2024
a2718a6
address feedback
withinboredom Aug 2, 2024
714a445
grammar
withinboredom Aug 2, 2024
12d702d
clarity
withinboredom Aug 2, 2024
57109f5
refactor reflection and make it make more sense
withinboredom Aug 2, 2024
1b91037
address feedback
withinboredom Aug 2, 2024
91d287e
remove trailing space
withinboredom Aug 2, 2024
ce74737
add information about non-trivial values
withinboredom Aug 2, 2024
30d19d9
make comparisons undefined
withinboredom Aug 2, 2024
c1d0a8a
add function autoloading
withinboredom Aug 15, 2024
a320634
reword things
withinboredom Aug 15, 2024
908d482
update example
withinboredom Aug 15, 2024
5134c01
clarify more
withinboredom Aug 15, 2024
72f3856
further clarification
withinboredom Aug 15, 2024
1b50121
define function_exists
withinboredom Aug 15, 2024
1020c04
fix a typo
withinboredom Aug 16, 2024
5b3a6c5
add mismatched-arguments
withinboredom Aug 18, 2024
df2e506
always pull
withinboredom Aug 18, 2024
eb437b9
remove whitespace
withinboredom Aug 18, 2024
a6ff51d
always pull
withinboredom Aug 18, 2024
16e6967
apparently, spl_autoload does need some changes
withinboredom Aug 18, 2024
c6cbaf0
updated rfc
withinboredom Nov 16, 2024
6cd68e6
updated rfc
withinboredom Nov 16, 2024
fdb8dd6
Automated changes from GitHub Actions
github-actions[bot] Nov 16, 2024
f14b093
updated author
withinboredom Nov 16, 2024
c7155c3
added interface documentation
withinboredom Nov 16, 2024
02ed26f
update records
withinboredom Nov 16, 2024
be0024a
move to under discussion
withinboredom Nov 16, 2024
c2e5eef
new functions
withinboredom Nov 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .coderabbit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tone_instructions: ''
early_access: false
enable_free_tier: true
reviews:
profile: chill
profile: assertive
request_changes_workflow: false
high_level_summary: true
high_level_summary_placeholder: '@coderabbitai summary'
Expand All @@ -18,7 +18,7 @@ reviews:
path_instructions: [
{
"path": "drafts/*.md",
"instructions": "These are PHP RFC's to change the PHP language. Be constructive but critical in how it may change the language"
"instructions": "These are PHP RFC's to change the PHP language. Do not just consider the grammar of the text, but consider how it might change the language. For example, if a new feature is being added, consider how it might be used, and propose better ideas if you have them."
}
]
abort_on_close: true
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ all: $(PUBLISHED) .git/hooks/pre-commit

drafts/template.md: template.ptxt
@echo "Creating draft from template"
src/convert-to-md.sh template.txt drafts/template.md
src/convert-to-md.sh template.ptxt drafts/template.md

published/%.ptxt: drafts/%.md
@echo "Converting $< to $@"
Expand Down
Empty file removed drafts/.gitkeep
Empty file.
173 changes: 173 additions & 0 deletions drafts/function-autoloading.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# PHP RFC: Function Autoloading v4

* Version: 1.0
* Date: 2024-08-15
* Author: Robert Landers, [email protected]
* Status: Under Discussion (or Accepted or Declined)
* First Published at: <http://wiki.php.net/rfc/function_autoloading4>

## Introduction

The topic of supporting function autoloading was brought up many times in the past, this RFC introduces a potential
implementation which would be consistent with what we have for autoloading classes.

## Proposal

Before getting into the details,
there are a few terms worth acknowledging so that the proposal can be easily discussed without getting confused:

1. **Defined function**: A function that the engine has knowledge of, such as in a previously included/required file.
2. **Undefined function**: A function that the engine does not have knowledge of.
3. **Function autoloading**: The process of loading a function that is not defined.
4. **Written function**: A function that exists in a file that the engine may or may not have knowledge of.
5. **Local scope**: The current namespace
6. **Global scope**: The global namespace (`\`)

The suggested change would be pretty straightforward and backwards-compatible:

1. Add two new constants to spl: SPL_AUTOLOAD_CLASS, SPL_AUTOLOAD_FUNCTION.
2. Add a fourth optional parameter for spl_autoload_register, with a default value of SPL_AUTOLOAD_CLASS.
3. The type for the missing token should also be passed to the $autoload_function callback as a second param. (e.g.,
SPL_AUTOLOAD_CLASS for classes, SPL_AUTOLOAD_FUNCTION for functions)
4. Change the current class autoloading to only call the autoloaders which match with the SPL_AUTOLOAD_CLASS types.
5. Add the function autoloading to only call the autoloaders which match with the SPL_AUTOLOAD_FUNCTION types.

There won’t be any changes to the current autoloading mechanism when it comes to classes.
However, if a function

1. is called in a fully qualified form (e.g., a `use` statement or `\` prefix is used),
2. is not defined,
3. and an autoloader is registered with the SPL_AUTOLOAD_FUNCTION type

then the autoloader will be called with the function name as the first parameter (with the initial slash removed) and
SPL_AUTOLOAD_FUNCTION as the second parameter.

However, if a function

1. is called in an unqualified form (e.g., `strlen()`),
2. is not defined locally
3. and an autoloader is registered with the SPL_AUTOLOAD_FUNCTION type

then the autoloader will be called with the current namespace prepended to the function name.
If the autoloader chooses to look up the "basename" of the function, it may do so.
If the function is still undefined in the local scope,
then it will fall back to the global scope—unless the local scope is the global scope.
The function autoloader will not be called again.

This provides an opportunity
for an autoloader to check for the existence of a function in the local scope and define it,
as well as defer to the global scope if it is not defined.

Example "`PSR-4-style`" (except the last part of the namespace is the file it is in) function autoloader:

```php
<?php

spl_autoload_register(function ($function, $type) {
if ($type === SPL_AUTOLOAD_FUNCTION) {
$function_path = dirname(str_replace('\\', DIRECTORY_SEPARATOR, $function));
$file = __DIR__ . '/functions/' . $function_path . '.php';

if (file_exists($file)) {
require_once $file;
}
}
}, false, false, SPL_AUTOLOAD_FUNCTION);
```

Performance-wise, this should have minimal impact on existing codebases as there is no default function autoloader.

For codebases that want to take advantage of function autoloading,
it may be desirable to stick with FQNs for functions and/or employ caches and other techniques where possible.

### spl_autoload

`spl_autoload`'s second argument will be updated to accept `int|string|null` as the second parameter so that it can use
the new callback signature.
If the second parameter is an int, and it is not `SPL_AUTOLOAD_CLASS`,
an `Error` is thrown: 'Default autoloader can only load classes.'

There will not be a default function autoloader.

### spl_autoload_call

The `spl_autoload_call` function will be modified to accept a second parameter of one,
(but not both) of the new constants,
with the default value set to SPL_AUTOLOAD_CLASS.
The name of the first parameter will be changed to `$name` to reflect that it can be a class or function name.

```php
spl_autoload_call('\Some\func', SPL_AUTOLOAD_FUNCTION); // Calls the function autoloader
spl_autoload_call('\Some\func'); // Calls the class autoloader
spl_autoload_call('Some\func', SPL_AUTOLOAD_CLASS); // Calls the class autoloader
spl_autoload_call('Some\func'); // Calls the class autoloader
spl_autoload_call('func', SPL_AUTOLOAD_FUNCTION | SPL_AUTOLOAD_CLASS); // Error: Cannot autoload multiple types
```

If the user wants to call multiple autoloaders, they can do so manually.

### function_exists

The `function_exists` function will be updated to include a boolean option (`$autoload`) as the second parameter,
which will default to `true`.
If set to `true`, the function autoloader will be called if the function is not defined, otherwise, it will not be
called.

## Backward Incompatible Changes

### Mismatched arguments

If an autoloader was registered that can accept more than one argument,
it may fail or perform unexpected behavior when it receives a second argument of `SPL_AUTOLOAD_CLASS`.

## Proposed PHP Version(s)

8.5 or later.

## RFC Impact

### To Opcache

To be determined.

### New Constants

Two new constants will be added to the SPL extension: SPL_AUTOLOAD_CLASS, SPL_AUTOLOAD_FUNCTION.

## Open Issues

To be determined.

## Future Scope

Potentially, constants and stream wrappers can be added in a similar fashion.

## Proposed Voting Choices

<doodle title="Implement Function Autoloading v4, as described" auth="withinboredom" voteType="single" closed="true" closeon="2022-01-01T00:00:00Z">
* Yes
* No
</doodle>

## Patches and Tests

Not yet.

## Implementation

After the project is implemented, this section should contain - the
version(s) it was merged into - a link to the git commit(s) - a link to
the PHP manual entry for the feature - a link to the language
specification section (if any)

## References

- [autofunc](https://wiki.php.net/rfc/autofunc): This heavily influenced this RFC. (declined in 2011)
- [function_autoloading](https://wiki.php.net/rfc/function_autoloading): This RFC was declined in 2011.
- [function_autoloading_v2](https://wiki.php.net/rfc/function_autoloading2): This RFC was declined in 2012.

Thank you for all of those that contributed to the discussions back then. I hope that this RFC will be successful.

## Rejected Features

Keep this updated with features that were discussed on the mail lists.
Loading
Loading