From 84ef00c175241c4c10f3f87c35d7c457e2b184a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=BDoljom?= Date: Sat, 20 Aug 2022 15:52:34 +0200 Subject: [PATCH] Add object instantiation chapter --- wordpress-coding-standards/php.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/wordpress-coding-standards/php.md b/wordpress-coding-standards/php.md index 69f17b8..f6f7178 100644 --- a/wordpress-coding-standards/php.md +++ b/wordpress-coding-standards/php.md @@ -755,6 +755,23 @@ abstract class Foo { } ``` +### Object instantiation + +When instantiating a new object instance, parenthesis must always be used, even when not strictly necessary. +There should be no space between the name of the class being instantiated and the opening parenthesis. +Assigning the return value of an object instantiation by reference is not allowed (new by reference has not been supported by PHP for quite a long while now). + +```php +// Correct. +$foo = new Foo(); +$anonymous_class = new class() { ... }; +$instance = new static(); + +// Incorrect. +$foo = & new Foo; +$foo = new Foo (); +``` + ## Control Structures ### Use `elseif`, not `else if`