Skip to content

Instantly share code, notes, and snippets.

@brzuchal
Last active November 23, 2016 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brzuchal/8d44b1b8f312dd1ad2ebefc4cec150ae to your computer and use it in GitHub Desktop.
Save brzuchal/8d44b1b8f312dd1ad2ebefc4cec150ae to your computer and use it in GitHub Desktop.
Nested/Inner classes playground
<?php
class Person
{
private interface Name
{
public function __construct(string $name);
public function __toString() : string;
}
private class FirstName implements self\Name
{
private $name;
public function __construct(string $name) {
// validation
if (strlen($name) < 3) {
throw new InvalidargumentException("First name should have min 3 letters long, given: {$name}");
}
$this->name = $name;
}
public function __toString() : string {
return $this->name;
}
}
private class SecondName implements self\Name
{
private $name;
public function __construct(string $name) {
// it would be reasonable to have access to owning object but `parent` already taken by super class
// shouldn't `parent` be renamed to `super`!
// then `$parent` could be owning object `$this`
if (is_null($parent->firstName)) {
throw new RuntimeException("It is impossible to have second name without firstname");
}
// validatiojn
if (strlen($name) < 1) {
throw new InvalidargumentException("Second name should have min 1 letter long, given: {$name}");
}
$this->name = $name;
}
public function __toString() : string
{
return $this->name;
}
}
/** @var self\FirstName */
private $firstName;
/** @var self\SecondName */
private $secondName;
public function __construct(string $fname, string $sname) {
$this->firstName = new $this\FirstName($fname);
$this->secondName = new $this\SecondName($sname);
}
public function getName() : string {
return "{$this->firstName} {$this->secondName}";
}
}
<?php
class Stock implements IteratorAggregate
{
public class Option
{
private $name;
private $value;
public function __construct(string $name, int $value)
{
if (strlen($name) < 3) {
throw new InvalidArgumentException("Stock option name requires min. 3 letters long, given: {$name}");
}
if ($value <= 0) {
throw new InvalidArgumentException("Stock option must have positive value, given: {$value}");
}
$this->name = $name;
$this->value = $value;
// if there were access to owning `$this` by `$parent`
// then could automatically add to options table
$parent->options[] = $this;
}
}
private $options = [];
public function getIterator() {
return new ArrayIterator($this->options);
}
}
$stock = new Stock();
$option = new $stock\Option("MAD", 1500);
var_dump($stock);
// class Stock#1 (1) {
// private $options =>
// array(1) {
// [0] =>
// class Stock#1Option#2 (2) {
// private $name =>
// string(3) "MAD"
// private $value =>
// int(1500)
// }
// }
// }
$incorectlyNamedOption = new $stock\Option("a", 1);
// InvalidArgumentException: Stock option name requires min. 3 letters long, given: a
$incorectValueOption = new $stock\Option("ORA", -1);
// InvalidArgumentException: Stock option must have positive value, given: -1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment