[TYPO3-core] Coding Guidelines about PHP Namespaces
Jigal van Hemert
jigal.van.hemert at typo3.org
Sun Mar 24 13:15:39 CET 2013
Hi,
On 24-3-2013 10:41, Benjamin Mack wrote:
> as I recently have some commits flying around where "bugfixes" introduce
> the namespaces declaration "use" like this
>
> namespace TYPO3\CMS\Frontend\ContentObject;
> use TYPO3\CMS\Core\Utility\GeneralUtility;
>
> I feel like we should have some kind of definition when or for what kind
> of namespaces we want to use "use", but then do it everywhere where it
> makes sense. This info should be added to the official CGL, otherwise
> we'll have some really dirty code, even after the namespaces cleanup ;-).
>
> I suggest: Every core class has the following lines
>
> use TYPO3\CMS\Core as CMS;
> use TYPO3\CMS\Core\Utility as Utility;
> use TYPO3\CMS\Backend as Backend;
> use TYPO3\CMS\Frontend as Frontend;
>
> I'm not a namespace expert but this seems logical for me to make the
> code more descriptive.
There is not functional need to use "use", but it makes the code more
readable. Instead of having long, complete namespaces only a single name
is used.
However, it seems rather confusing to use a different name instead of
the class name.
use TYPO3\CMS\Frontend as Frontend;
is superfluous as it is equal to:
use TYPO3\CMS\Frontend;
> I could also live with "don't use "use" at all".
>
> My other option to make the cleanup complete:
> use TYPO3\CMS\Core as t3lib;
> ;-)
In general it seems sensible to me to:
- import a namespace if it is used more than approx. three times in a file
- don't use aliases unless there are conflicts
- don't import namespaces that are not used
- order the imports alphabetically
- only use single imports (no multiple imports in a single "use" statement)
--
Jigal van Hemert
TYPO3 CMS Core Team member
TYPO3 .... inspiring people to share!
Get involved: typo3.org
More information about the TYPO3-team-core
mailing list