Skip to content

Conversation

@ravage84
Copy link
Contributor

@ravage84 ravage84 commented Jun 5, 2023

While I like the consistency of having opening tags everywhere (as in #74), I can see it being "too much", especially for one liners.

What do you think of the following rule of thumb proposal?

Add PHP opening tags when all requirements are met:

  • Tne mandatory class namespace is defined or no class or interface respectively is defined
  • All necessary classes & functions are imported
  • All necessary variables & constants are defined
  • All necessary structures are visible or hinted at with a comment
  • All statements and/or structures are related to each other
  • Or the PHP opening tag is essential to the code example

Do not add PHP opening tags when:

  • Only one or many unrelated statements are shown
  • Only one or many unrelated functions are shown
  • Only one or many unrelated methods are shown
  • Only one or many unrelated classes or interfaces respectively are shown
  • Only one or many unrelated other structures, such as an if-clause or match expression, are shown
Add PHP opening tags when all requirements are met: - Tne [mandatory class namespace](https://www.php-fig.org/psr/psr-1/#3-namespace-and-class-names) is defined or no class or interface respectively is defined - All necessary classes & functions are imported - All necessary variables & constants are defined - All necessary structures are visible or hinted at with a comment - All statements and/or structures are related to each other - Or the PHP opening tag is essential to the code example Do not add PHP opening tags when: - Only one or many unrelated statements are shown - Only one or many unrelated functions are shown - Only one or many unrelated methods are shown - Only one or many unrelated classes or interfaces respectively are shown - Only one or many unrelated other structures, such as an if-clause or match expression, are shown

```php
<?php

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd leave this one. We're talking about the use statements, which happen at a very specific place in the file, so I think it's fine to leave here.

@vjik
Copy link
Contributor

vjik commented Sep 8, 2023

Suggest shorten the list when need add PHP opening tag to several items:

  • block contain code with several sections (for example, class declaration + use) that can be placed whole in file;
  • the PHP opening tag is essential to the code example.

In otherwise cases <?php looks overhead.

@Crell
Copy link
Collaborator

Crell commented May 31, 2025

Looks like nothing is happening here.

@Crell Crell closed this May 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants