Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require/import vs require/include in PHP Coding Standards Documentation. - Fix for #143 #144

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
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
2 changes: 1 addition & 1 deletion wordpress-coding-standards/php.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ Group `use` statements are available from PHP 7.0, and trailing commas in group
[/alert]

[info]
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load whatever is being imported. You'll either need to set up autoloading or load the file containing the class/function/constant using a `require/import` statement, for the imported constructs to be loaded when used.
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load referenced classes. You'll either need to set up autoloading or load the file containing the class using a `require/include` statement, for the imported classes to be loaded when used. Autoloading is only applicable to classes; for functions and constants, you must always use `require` or `include`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load referenced classes. You'll either need to set up autoloading or load the file containing the class using a `require/include` statement, for the imported classes to be loaded when used. Autoloading is only applicable to classes; for functions and constants, you must always use `require` or `include`.
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load referenced classes. You'll either need to set up autoloading or load the file containing the class using a `require/include[_once]` statement. Autoloading is only applicable to classes; for functions and constants, you must always use `require[_once]` or `include[_once]`.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely like the added [_once] in the new sentence. I'll already apply that second part.

I am personally unsure about the require/include[_once].

I personally read it as "'require/includestatement", and due to that "statement" I included the optional_once` for both.
If you want to make it clear, I personally would suggest:

[...] or load the file containing the class using require[_once] or include[_once]. [...]

Imho that improves readability. Otherwise I personally would keep require/include.

Your thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrfnl I've added the before mentioned change. And changed order to alphabetical (first include then require) this matches the paragraph on "Writing include/require statements" in lines 81-83 of the same file.

Let me know your thoughts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order was intentional - require should generally be preferred over include, which is why it was mentioned first.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrfnl I agree with that point. What do you think about adjusting the other appearance of include/require to require/include, to match the same idea?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd need to look at the complete doc to be sure (on the road now, so can't look), but in principle, I'd be open to such a change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrfnl I've updated the order, and I've added a subsentence to the documentation in the upper paragraph. My experience showed that include statements can result in silent malfunctions, which are a pain to track down. If you disagree adding this, I'm absolutely fine to omit that.

[/info]

**Note about WordPress Core usage**
Expand Down