Skip to content

Instantly share code, notes, and snippets.

@SebSept
Created May 7, 2020 16:10
Show Gist options
  • Save SebSept/38d23301be75a134ee27e0eb850a5025 to your computer and use it in GitHub Desktop.
Save SebSept/38d23301be75a134ee27e0eb850a5025 to your computer and use it in GitHub Desktop.
immutable, not hackable by the __construct()
<?php
class imu
{
/**
* @var float
*/
private $one;
/**
* @var float
*/
private $two;
private function __construct(float $one, float $two)
{
$this->one = $one;
$this->two = $two;
}
public static function createFromFloats(float $one, float $two): self
{
return new static($one, $two);
}
public function __toString()
{
return sprintf("one : %f & two : %f", $this->one, $this->two);
}
}
$imu = imu::createFromFloats(5.0, 0.0);
echo $imu;
@SebSept
Copy link
Author

SebSept commented May 7, 2020

final class imu serait mieux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment