Skip to content

Instantly share code, notes, and snippets.

@nyamsprod
Created June 24, 2022 09:18
Show Gist options
  • Save nyamsprod/af49f73749f2a61e7618d844d8f4f1f3 to your computer and use it in GitHub Desktop.
Save nyamsprod/af49f73749f2a61e7618d844d8f4f1f3 to your computer and use it in GitHub Desktop.
type-hint and PHP
<?php
function getReport(CarbonInterface $start, CarbonInterface $end): Report
{
return Report::query()
->whereDate('status_date', '>=', $start)
->whereDate('status_date', '<', $end)
}
<?php
function getReport(DateTimeInterface $start, DateTimeInterface $end): Report
{
return Report::query()
->whereDate('status_date', '>=', $start)
->whereDate('status_date', '<', $end)
}
<?php
function changeDate(Carbon $date): Carbon
{
return $date->addDays(3);
}
<?php
function changeDate(CarbonImmutable $date): CarbonImmutable
{
return $date->addDays(3);
}
<?php
interface DateChanger
{
public function changeDate(DateTimeImmutable $date): DateTimeImmutable;
}
final class CarbonChanger implements DateChanger
{
public function changeDate(DateTimeImmutable $date): DateTimeImmutable
{
return CarbonImmutable::fromInterface($date)->addDays(3);
}
}
<?php
function getReport(Carbon $start, Carbon $end): Report
{
return Report::query()
->whereDate('status_date', '>=', $start)
->whereDate('status_date', '<', $end)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment