{"id":8712,"date":"2020-05-11T11:27:03","date_gmt":"2020-05-11T02:27:03","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=8712"},"modified":"2020-05-07T14:30:52","modified_gmt":"2020-05-07T05:30:52","slug":"trait-in-php-object-oriented-programming","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/8712","title":{"rendered":"Trait in PHP Object Oriented Programming"},"content":{"rendered":"\n<p><font style=\"vertical-align: inherit\"><font style=\"vertical-align: inherit\">Today I will talk about trait and its sample usages.In the Object Oriented Inheritance, a child class can only extend only one parent class.So what if the child class has to extend more than one parent classes? OOP trait is the answer for this problem as child classes can extend many traits as they want.Check out the samples below.  <\/font><\/font><\/p>\n\n\n\n<p>Here is the sample usage of a trait<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ntrait Trait1 {\n  public function print() {\n    echo \"lets extend more than one trait!\"; \n  }\n}\n\nclass Home {\n  use Trait1;\n}\n\n$obj = new Home();\n$obj->print();\n?><\/code><\/pre>\n\n\n\n<p>Now, Lets extend more than one trait.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ntrait Trait1 {\n  public function print1() {\n    echo \"I am the trait1\"; \n  }\n}\n\ntrait Trait2 {\n  public function print2() {\n    echo \"I am the trait2\"; \n  }\n}\n\nclass Home {\n  use Trait1,Trait2;\n}\n\n$obj = new Home();\n$obj->print1();\n$obj->print2();\n?><\/code><\/pre>\n\n\n\n<p>By Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8712\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/8712\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Trait in PHP Object Oriented Programming\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8712\" data-text=\"Trait in PHP Object Oriented Programming\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8712\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8712\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Today I will talk about trait and its sample usages.In the Object Oriented Inheritance, a child class can only [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8712"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=8712"}],"version-history":[{"count":3,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8712\/revisions"}],"predecessor-version":[{"id":8752,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8712\/revisions\/8752"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=8712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=8712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=8712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}