{"id":1769,"date":"2024-08-03T08:15:54","date_gmt":"2024-08-03T06:15:54","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1769"},"modified":"2024-08-03T08:15:55","modified_gmt":"2024-08-03T06:15:55","slug":"09-interfaces-y-herencia-multiple-con-php","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/php\/poo-php\/09-interfaces-y-herencia-multiple-con-php\/","title":{"rendered":"09. Interfaces y herencia m\u00faltiple con PHP"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Como dijimos en anteriores cap\u00edtulos,&nbsp;<strong>PHP<\/strong>&nbsp;no tiene la capacidad de hacer&nbsp;<strong>herencia m\u00faltiple<\/strong>. Para resolver el problema de la herencia m\u00faltiple, PHP utiliza las interfaces, que como vimos en cap\u00edtulos anteriores, son un conjunto de m\u00e9todos caracter\u00edsticos de diversas&nbsp;<strong>clases<\/strong>, independientemente de la relaci\u00f3n que dichas&nbsp;<strong>clases<\/strong>&nbsp;mantengan entre s\u00ed, por lo que una misma&nbsp;<strong>clase<\/strong>&nbsp;puede implementar m\u00faltiples&nbsp;<strong>interfaces<\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">Las&nbsp;<strong>interfaces<\/strong>&nbsp;se definen utilizando la palabra clave&nbsp;<strong><em>interface<\/em><\/strong>, y se pueden emplear utilizando la palabra clave&nbsp;<strong><em>implements<\/em><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Las&nbsp;<strong>interfaces<\/strong>, aunque no son&nbsp;<strong>clases<\/strong>, en&nbsp;<strong>PHP<\/strong>&nbsp;son interpretadas como tal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Caracter\u00edsticas de las interfaces<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Como vimos en cap\u00edtulos anteriores, a modo de recordatorio:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No pueden tener el mismo nombre que una\u00a0<strong>clase<\/strong>.<\/li>\n\n\n\n<li>No pueden instanciarse porque no tienen referencias asociadas a\u00a0<strong>objetos<\/strong>.<\/li>\n\n\n\n<li>No se pueden definir\u00a0<strong>propiedades<\/strong>, s\u00f3lo van a tener\u00a0<strong>m\u00e9todos<\/strong>.<\/li>\n\n\n\n<li>Los\u00a0<strong>m\u00e9todos<\/strong>\u00a0de una\u00a0<strong>interfaz<\/strong>\u00a0deben ser\u00a0<strong>p\u00fablicos<\/strong>, s\u00f3lo estar definidos y no tener cuerpo.<\/li>\n\n\n\n<li>Las\u00a0<strong>clases<\/strong>\u00a0que implementan una\u00a0<strong>interfaz<\/strong>\u00a0deben utilizar todos los\u00a0<strong>m\u00e9todos<\/strong>\u00a0de la misma.<\/li>\n\n\n\n<li>Diferentes\u00a0<strong>interfaces<\/strong>\u00a0no pueden tener nombres de m\u00e9todos id\u00e9nticos si son implementadas por una misma\u00a0<strong>clase<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\ninterface Ingredientes {\n&nbsp; &nbsp; public function establecer_ingredientes($lista);\n&nbsp; &nbsp; public function obtener_ingredientes();\n}\n\ninterface Receta {\n&nbsp; &nbsp; public function establecer_receta($pasos);\n&nbsp; &nbsp; public function obtener_receta();\n}\n\nclass Postre implements Ingredientes, Receta {\n&nbsp; &nbsp; private $ingredientes;\n&nbsp; &nbsp; private $recetas;\n\n&nbsp; &nbsp; public function establecer_ingredientes($lista){\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;ingredientes = explode(',', $lista);\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function obtener_ingredientes(){\n&nbsp; &nbsp; &nbsp; &nbsp; $num_ingredientes = count($this-&gt;ingredientes);\n&nbsp; &nbsp; &nbsp; &nbsp; $html = '&lt;ul&gt;';\n&nbsp; &nbsp; &nbsp; &nbsp; for ($i=0; $i &lt; $num_ingredientes; $i++) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $html .= '&lt;li&gt;' . $this-&gt;ingredientes[$i] . '&lt;\/li&gt;';\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; $html .= '&lt;\/ul&gt;';\n&nbsp; &nbsp; &nbsp; &nbsp; return print($html);\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function establecer_receta($pasos){\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;receta = explode(\"|\", $pasos);\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function obtener_receta(){\n&nbsp; &nbsp; &nbsp; &nbsp; $num_pasos = count($this-&gt;receta);\n&nbsp; &nbsp; &nbsp; &nbsp; $html = '&lt;ol&gt;';\n&nbsp; &nbsp; &nbsp; &nbsp; for ($i=0; $i &lt; $num_pasos; $i++) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $html .= '&lt;li&gt;' . $this-&gt;receta[$i] . '&lt;\/li&gt;';\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; $html .= '&lt;\/ol&gt;';\n&nbsp; &nbsp; &nbsp; &nbsp; return print($html);\n&nbsp; &nbsp; }\n}\n\n$bizcocho = new Postre();\necho \"&lt;h1&gt;Postres&lt;\/h1&gt;\";\necho \"&lt;h2&gt;Bizcocho&lt;\/h2&gt;\";\n\necho \"&lt;h3&gt;Ingredientes&lt;\/h3&gt;\";\n$bizcocho-&gt;establecer_ingredientes('\n1 vaso de harina,\n1 huevo,\n1\/3 vaso de leche,\n3 cucharadas mantequilla\n');\n$bizcocho-&gt;obtener_ingredientes();\n\necho \"&lt;h3&gt;Receta&lt;\/h3&gt;\";\n$bizcocho-&gt;establecer_receta('\n&nbsp; &nbsp; Mezclar todos los ingredientes exceptuando la mantequilla\n&nbsp; &nbsp; en un recipiente hasta tener una masa espesa y uniforme|\n&nbsp; &nbsp; Calentar 1 cucharada de mantequilla a fuego lento en una\n&nbsp; &nbsp; sart\u00e9n|\n&nbsp; &nbsp; Cuando se derrita la mantequilla, verter la mezcla|\n&nbsp; &nbsp; Dejar calentar la mezcla hasta que salgan burbujas|\n&nbsp; &nbsp; Cuando la consistencia se vea esponjosa dar la vuelta\n');\n$bizcocho-&gt;obtener_receta();\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Como dijimos en anteriores cap\u00edtulos,&nbsp;PHP&nbsp;no tiene la capacidad de hacer&nbsp;herencia m\u00faltiple. Para resolver el problema de la herencia m\u00faltiple, PHP utiliza las&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1735,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","footnotes":""},"class_list":["post-1769","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"Como dijimos en anteriores cap\u00edtulos,&nbsp;PHP&nbsp;no tiene la capacidad de hacer&nbsp;herencia m\u00faltiple. Para resolver el problema de la herencia m\u00faltiple, PHP utiliza las...","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=1769"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1769\/revisions"}],"predecessor-version":[{"id":1771,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1769\/revisions\/1771"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1735"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=1769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}