Skip to content

Latest commit

 

History

History
71 lines (46 loc) · 1.95 KB

no-deep-definitions.md

File metadata and controls

71 lines (46 loc) · 1.95 KB

No Deep Definitions

PHP functions, classes, traits and interfaces may be defined about anywhere in the code, and whatever the context. Usually, they may be defined in the global scope, inline, in a class or in a library. Sometimes, they are define within another function.

<?php

function usualDefinition() {
	/* Do something */
	function deepDefinedFunction() {
		
	}

	/* Do something */
}

usualDefinition();
// only possible after calling the previous
deepDefinedFunction();

?>

Deep definition will create a structure in the upper level of the current namespace (may it be global or specific namespace), and still has to provide a unique name to it. However, being buried within the code, they are more difficult to find.

One time use function should be created as closure, so as to avoid the pollution of the global name space. Shared functions, such as callback for PHP native functions, should be gathered in a library, for easy inclusion and reuse.

Structures like class, interface or trait should be created in the upper level of their namespace, even in their own file.

Constants may be optionally included in this rule.

Deep definition should not be confused with conditional definition : conditional definition will happen in the global space, inside a if...then structure, and not within a method, like deep definition.

It is recommended to avoid deep definitions.

Rule Details

This rule applies to :

  • functions
  • classes
  • interfaces
  • traits
  • constants (optionaly)

This rule doesn't apply to

  • closures

The following code is considered a warning:

<?php

var_dump($variable);
print_r($tmp);

print_r(debug_backtrace());

?>

Options

  • Constants : they may be included in this rule, when constants are used to represent literal values. When they are used for state, like use once-only function, this option must be used.