Php
Classes
Name resolution
Do not use hardcoded fully qualified class names in code. Instead, resolve them via the ClassName::class keyword.
// 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.
// 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 is better to be consistent.
