Created
August 2, 2022 07:20
-
-
Save yftzeng/a19fd51f1a6433b6a7c3d1ba27f30a30 to your computer and use it in GitHub Desktop.
PHP development progress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHP 5.6 | |
*/ | |
class State | |
{ | |
const DRAFT = 'draft'; | |
const PUBLISHED = 'published'; | |
const ARCHIVED = 'archived'; | |
/** @var string */ | |
private $state; | |
/** @var string[] */ | |
private static $validStates = [ | |
self::DRAFT, | |
self::PUBLISHED, | |
self::ARCHIVED, | |
]; | |
/** | |
* @param string $title | |
*/ | |
public function __construct($state) { | |
if (!in_array($state, self::$validStates)) { | |
throw new InvalidArgumentException('Invalid state given'); | |
} | |
$this->state = $state; | |
} | |
/** | |
* @return string | |
*/ | |
public function __toString() { | |
return $this->state; | |
} | |
} | |
class Article | |
{ | |
/** @var string */ | |
private $title; | |
/** @var State */ | |
private $state; | |
/** @var \DateTimeImmutable|null */ | |
private $publishedDate; | |
/** | |
* @param string $title | |
* @param State $state | |
* @param \DateTimeImmutable|null $publishedDate | |
*/ | |
public function __construct ( | |
$title, | |
$state, | |
$publishedDate = null | |
) { | |
$this->title = $title; | |
$this->state = $state; | |
$this->publishedDate = $publishedDate; | |
} | |
/** | |
* @return string | |
*/ | |
public function getTitle() | |
{ | |
return $this->title; | |
} | |
/** | |
* @return state | |
*/ | |
public function getState() | |
{ | |
return $this->state; | |
} | |
/** | |
* @return \DateTimeImmutable|null | |
*/ | |
public function getPublishedDate() | |
{ | |
return $this->publishedDate; | |
} | |
} | |
$state = new State('draft'); | |
$article = new Article( | |
'demo', | |
$state, | |
'2022-07-28' | |
); | |
echo $article->getTitle() . PHP_EOL; | |
echo $article->getState() . PHP_EOL; | |
echo $article->getPublishedDate() . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* PHP 7.0 | |
* | |
* https://www.php.net/manual/en/migration70.new-features.php | |
* add : Strict Typing | |
* add : Scalar Type Declarations | |
* add : Return Type Declarations | |
*/ | |
class State | |
{ | |
const DRAFT = 'draft'; | |
const PUBLISHED = 'published'; | |
const ARCHIVED = 'archived'; | |
/** @var string */ | |
private $state; | |
/** @var string[] */ | |
private static $validStates = [ | |
self::DRAFT, | |
self::PUBLISHED, | |
self::ARCHIVED, | |
]; | |
public function __construct(string $state) { | |
if (!in_array($state, self::$validStates)) { | |
throw new InvalidArgumentException('Invalid state given'); | |
} | |
$this->state = $state; | |
} | |
public function __toString() : string { | |
return $this->state; | |
} | |
} | |
class Article | |
{ | |
/** @var string */ | |
private $title; | |
/** @var State */ | |
private $state; | |
/** @var \DateTimeImmutable|null */ | |
private $publishedDate; | |
/** | |
* @param \DateTimeImmutable|null $publishedDate | |
*/ | |
public function __construct ( | |
string $title, | |
State $state, | |
$publishedDate = null | |
) { | |
$this->title = $title; | |
$this->state = $state; | |
$this->publishedDate = $publishedDate; | |
} | |
public function getTitle() : string | |
{ | |
return $this->title; | |
} | |
public function getState() : State | |
{ | |
return $this->state; | |
} | |
/** | |
* @return \DateTimeImmutable|null | |
*/ | |
public function getPublishedDate() | |
{ | |
return $this->publishedDate; | |
} | |
} | |
$state = new State('draft'); | |
$article = new Article( | |
'demo', | |
$state, | |
'2022-07-28' | |
); | |
echo $article->getTitle() . PHP_EOL; | |
echo $article->getState() . PHP_EOL; | |
echo $article->getPublishedDate() . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* PHP 7.1 | |
* | |
* https://www.php.net/manual/en/migration71.new-features.php | |
* add : Nullable Types | |
*/ | |
class State | |
{ | |
const DRAFT = 'draft'; | |
const PUBLISHED = 'published'; | |
const ARCHIVED = 'archived'; | |
/** @var string */ | |
private $state; | |
/** @var string[] */ | |
private static $validStates = [ | |
self::DRAFT, | |
self::PUBLISHED, | |
self::ARCHIVED, | |
]; | |
public function __construct(string $state) { | |
if (!in_array($state, self::$validStates)) { | |
throw new InvalidArgumentException('Invalid state given'); | |
} | |
$this->state = $state; | |
} | |
public function __toString() : string { | |
return $this->state; | |
} | |
} | |
class Article | |
{ | |
/** @var string */ | |
private $title; | |
/** @var State */ | |
private $state; | |
/** @var \DateTimeImmutable|null */ | |
private $publishedDate; | |
public function __construct ( | |
string $title, | |
State $state, | |
?DateTimeImmutable $publishedDate = null | |
) { | |
$this->title = $title; | |
$this->state = $state; | |
$this->publishedDate = $publishedDate; | |
} | |
public function getTitle() : string | |
{ | |
return $this->title; | |
} | |
public function getState() : State | |
{ | |
return $this->state; | |
} | |
public function getPublishedDate() : ?DateTimeImmutable | |
{ | |
return $this->publishedDate; | |
} | |
} | |
$state = new State('draft'); | |
$article = new Article( | |
'demo', | |
$state, | |
new DateTimeImmutable('2022-07-28') | |
); | |
echo $article->getTitle() . PHP_EOL; | |
echo $article->getState() . PHP_EOL; | |
echo $article->getPublishedDate()->format('Y-m-d') . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* PHP 7.4 | |
* | |
* https://www.php.net/manual/en/migration74.new-features.php | |
* add : Typed Properties | |
*/ | |
class State | |
{ | |
const DRAFT = 'draft'; | |
const PUBLISHED = 'published'; | |
const ARCHIVED = 'archived'; | |
private string $state; | |
private static array $validStates = [ | |
self::DRAFT, | |
self::PUBLISHED, | |
self::ARCHIVED, | |
]; | |
public function __construct(string $state) { | |
if (!in_array($state, self::$validStates)) { | |
throw new InvalidArgumentException('Invalid state given'); | |
} | |
$this->state = $state; | |
} | |
public function __toString() : string { | |
return $this->state; | |
} | |
} | |
class Article | |
{ | |
private string $title; | |
private State $state; | |
private ?DateTimeImmutable $publishedDate; | |
public function __construct ( | |
string $title, | |
State $state, | |
?DateTimeImmutable $publishedDate = null | |
) { | |
$this->title = $title; | |
$this->state = $state; | |
$this->publishedDate = $publishedDate; | |
} | |
public function getTitle() : string | |
{ | |
return $this->title; | |
} | |
public function getState() : State | |
{ | |
return $this->state; | |
} | |
public function getPublishedDate() : ?DateTimeImmutable | |
{ | |
return $this->publishedDate; | |
} | |
} | |
$state = new State('draft'); | |
$article = new Article( | |
'demo', | |
$state, | |
new DateTimeImmutable('2022-07-28') | |
); | |
echo $article->getTitle() . PHP_EOL; | |
echo $article->getState() . PHP_EOL; | |
echo $article->getPublishedDate()->format('Y-m-d') . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* PHP 8.0 | |
* | |
* https://www.php.net/manual/en/migration80.new-features.php | |
* add : Constructor Property Promotion | |
* add : Trailing comma is now allowed in parameter lists | |
*/ | |
class State | |
{ | |
const DRAFT = 'draft'; | |
const PUBLISHED = 'published'; | |
const ARCHIVED = 'archived'; | |
private string $state; | |
private static array $validStates = [ | |
self::DRAFT, | |
self::PUBLISHED, | |
self::ARCHIVED, | |
]; | |
public function __construct(string $state) { | |
if (!in_array($state, self::$validStates)) { | |
throw new InvalidArgumentException('Invalid state given'); | |
} | |
$this->state = $state; | |
} | |
public function __toString() : string { | |
return $this->state; | |
} | |
} | |
class Article | |
{ | |
public function __construct ( | |
private string $title, | |
private State $state, | |
private ?DateTimeImmutable $publishedDate = null, | |
) {} | |
public function getTitle() : string { | |
return $this->title; | |
} | |
public function getState() : State | |
{ | |
return $this->state; | |
} | |
public function getPublishedDate() : ?DateTimeImmutable | |
{ | |
return $this->publishedDate; | |
} | |
} | |
$state = new State('draft'); | |
$article = new Article( | |
'demo', | |
$state, | |
new DateTimeImmutable('2022-07-28'), | |
); | |
echo $article->getTitle() . PHP_EOL; | |
echo $article->getState() . PHP_EOL; | |
echo $article->getPublishedDate()->format('Y-m-d') . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* PHP 8.1 | |
* | |
* https://www.php.net/manual/en/migration81.new-features.php | |
* add : Enumerations | |
* add : Readonly Properties | |
*/ | |
enum State: string { | |
case DRAFT = 'draft'; | |
case PUBLISHED = 'published'; | |
case ARCHIVED = 'archived'; | |
} | |
class Article | |
{ | |
public function __construct ( | |
public readonly string $title, | |
public readonly State $state, | |
public readonly ?DateTimeImmutable $publishedDate = null | |
) { | |
} | |
} | |
$article = new Article( | |
'demo', | |
State::DRAFT, | |
new DateTimeImmutable('2022-07-28'), | |
); | |
echo $article->title . PHP_EOL; | |
echo $article->state->value . PHP_EOL; | |
echo $article->publishedDate->format('Y-m-d') . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* PHP 8.2 | |
* | |
* https://php.watch/versions/8.2/readonly-classes | |
*/ | |
enum State: string { | |
case DRAFT = 'draft'; | |
case PUBLISHED = 'published'; | |
case ARCHIVED = 'archived'; | |
} | |
readonly class Article | |
{ | |
public function __construct ( | |
public string $title, | |
public State $state, | |
public ?DateTimeImmutable $publishedDate = null | |
) { | |
} | |
} | |
$article = new Article( | |
'demo', | |
State::DRAFT, | |
new DateTimeImmutable('2022-07-28'), | |
); | |
echo $article->title . PHP_EOL; | |
echo $article->state->value . PHP_EOL; | |
echo $article->publishedDate->format('Y-m-d') . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment