From 5b1e69072b8e6b8b71b3514eb001add14e46b286 Mon Sep 17 00:00:00 2001 From: AnrDaemon Date: Sun, 4 Nov 2018 14:29:25 +0300 Subject: [PATCH 1/3] Fixed Caracter address. --- chargen.php | 3 +-- lib/character.php | 4 +--- lib/view.php | 11 ++++++----- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/chargen.php b/chargen.php index 2da63d4..e57d2c3 100644 --- a/chargen.php +++ b/chargen.php @@ -6,5 +6,4 @@ $person = new Character(); #print("$person->name [$person->gender] $person->upp_s\n"); $this_person = new View($person); -print("Your new crew: $this_person->to_string .\n"); -?> +print "Your new crew: $this_person\n"; diff --git a/lib/character.php b/lib/character.php index cae9a41..691bdd8 100644 --- a/lib/character.php +++ b/lib/character.php @@ -17,7 +17,7 @@ private function set_gender() { return rand(0,1) ? "F" : "M"; } private function upp_to_s(array $upp) { - $string = ''; + $string = ''; foreach ($upp as $s) { $string .= dechex($s); } @@ -74,5 +74,3 @@ private function go_to_the_gym(array $upp) { } } -?> - diff --git a/lib/view.php b/lib/view.php index f53901d..a525fa1 100644 --- a/lib/view.php +++ b/lib/view.php @@ -1,13 +1,14 @@ person = $person; } - public function to_string() { - $return_string = "$this->name [$this->gender] $this->upp_s"; + + public function __toString() { + $return_string = "{$this->person->name} [{$this->person->gender}] {$this->person->upp_s}"; return $return_string; } } -?> - From e19e8041755d32c4c40318f28e75a7715cba8f4d Mon Sep 17 00:00:00 2001 From: AnrDaemon Date: Sun, 4 Nov 2018 14:35:13 +0300 Subject: [PATCH 2/3] Why View knows how to *convert* character to string? --- lib/character.php | 6 +++--- lib/view.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/character.php b/lib/character.php index 691bdd8..b4c8b76 100644 --- a/lib/character.php +++ b/lib/character.php @@ -8,9 +8,9 @@ public function __construct($data = array()) { $this->upp_s = $this->upp_to_s($this->upp); } - public function to_s() { - $upp_s = $this->upp_to_s($this->upp); - print("$this->name [$this->gender] $upp_s \n"); + public function __toString() { + $return_string = "{$this->name} [{$this->gender}] {$this->upp_s}"; + return $return_string; } private function set_gender() { diff --git a/lib/view.php b/lib/view.php index a525fa1..53f127b 100644 --- a/lib/view.php +++ b/lib/view.php @@ -8,7 +8,7 @@ public function __construct(Character $person) { } public function __toString() { - $return_string = "{$this->person->name} [{$this->person->gender}] {$this->person->upp_s}"; + $return_string = "{$this->person}"; return $return_string; } } From a4d22d1303850b2de486a0a433b95840393806c3 Mon Sep 17 00:00:00 2001 From: AnrDaemon Date: Sun, 4 Nov 2018 14:37:44 +0300 Subject: [PATCH 3/3] Fixed Character/View relation Character knows how to stringify itself. View knows how to compose entire presentation. Still, __toString() for View is a dubious choice. --- chargen.php | 2 +- lib/view.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/chargen.php b/chargen.php index e4a5d20..06c3822 100644 --- a/chargen.php +++ b/chargen.php @@ -5,4 +5,4 @@ $person = new Character(); $this_person = new View($person); -print "Your new crew: {$this_person}\n"; +print $this_person; diff --git a/lib/view.php b/lib/view.php index 53f127b..fd6d3f5 100644 --- a/lib/view.php +++ b/lib/view.php @@ -8,7 +8,7 @@ public function __construct(Character $person) { } public function __toString() { - $return_string = "{$this->person}"; + $return_string = "Your new crew: {$this->person}\n"; return $return_string; } }