{"id":25664,"date":"2024-11-15T07:57:55","date_gmt":"2024-11-14T22:57:55","guid":{"rendered":"http:\/\/www.tyosuke20xx.com\/blog\/?p=25664"},"modified":"2024-11-15T07:57:57","modified_gmt":"2024-11-14T22:57:57","slug":"php-%e3%83%88%e3%83%ac%e3%82%a4%e3%83%88","status":"publish","type":"post","link":"http:\/\/www.tyosuke20xx.com\/blog\/?p=25664","title":{"rendered":"PHP \u30c8\u30ec\u30a4\u30c8"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\ntrait LogTrait\n{\n    public function log()\n    {\n        echo &quot;Instance created&quot; . PHP_EOL;\n    }\n}\n\nabstract class Score\n{\n    use LogTrait;\n\n    private $subject;\n    protected $points;\n\n    public function __construct($subject, $points)\n    {\n        $this-&gt;subject = $subject;\n        $this-&gt;points = $points;\n        $this-&gt;log();\n    }\n\n    abstract protected function getResult();\n\n    public function getInfo()\n    {\n        return &quot;{$this-&gt;subject}, {$this-&gt;points}, {$this-&gt;getResult()}&quot;;\n    }\n}\n\nclass MathScore extends Score\n{\n    public function __construct($points)\n    {\n        parent::__construct(&quot;Math&quot;, $points);\n    }\n\n    protected function getResult()\n    {\n        echo &quot;MathScore method&quot; . PHP_EOL;\n        return $this-&gt;points &gt;= 50 ? &quot;Pass&quot; : &quot;Fail&quot;;\n    }\n}\n\nclass EnglishScore extends Score\n{\n    public function __construct($points)\n    {\n        parent::__construct(&quot;English&quot;, $points);\n    }\n\n    protected function getResult()\n    {\n        echo &quot;EnglishScore method&quot; . PHP_EOL;\n        return $this-&gt;points &gt;= 95 ? &quot;Pass&quot; : &quot;Fail&quot;;\n    }\n}\n\nclass User\n{\n    use LogTrait;\n\n    private $name;\n    private $score;\n\n    public function __construct($name, $score)\n    {\n        $this-&gt;name = $name;\n        $this-&gt;score = $score;\n        $this-&gt;log();\n    }\n\n    public function getInfo()\n    {\n        return &quot;{$this-&gt;name}, {$this-&gt;score-&gt;getInfo()}&quot;;\n    }\n}\n\n$user1 = new User(&quot;Taro&quot;, new MathScore(70));\n$user2 = new User(&quot;Jiro&quot;, new EnglishScore(90));\n\necho $user1-&gt;getInfo() . PHP_EOL;\necho $user2-&gt;getInfo() . PHP_EOL;\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[50,4],"tags":[38,3],"class_list":["post-25664","post","type-post","status-publish","format-standard","hentry","category-php","category-programming","tag-php","tag-programming"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=25664"}],"version-history":[{"count":1,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25664\/revisions"}],"predecessor-version":[{"id":25665,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25664\/revisions\/25665"}],"wp:attachment":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25664"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}