per-coding-style icon indicating copy to clipboard operation
per-coding-style copied to clipboard

Only add PHP opening tag when the following rule of thumb are met

Open ravage84 opened this issue 2 years ago • 1 comments

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

ravage84 avatar Jun 05 '23 12:06 ravage84

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.

vjik avatar Sep 08 '23 07:09 vjik

Looks like nothing is happening here.

Crell avatar May 31 '25 06:05 Crell