Classes
Name resolution
Do not use hardcoded fully qualified class names in code. Instead, use resolution via ClassName::class keyword.
PHP
// GOOD
use App\Models\Order;
echo Order::class;
// BAD
echo 'App\Models\Order';
Self keyword
Use the class name instead of the self keyword for return type hints and when creating an instance of the class.
PHP
// GOOD
public function createFromName(string $name): Tag
{
$tag = new Tag();
$tag->name = $name;
return $tag;
}
// BAD
public function createFromName(string $name): self
{
$tag = new self();
$tag->name = $name;
return $tag;
}
return self/new self() vs return ClassName/new ClassName()—we can use both, but it would be good to be consistent
Table of Contents
