{"id":1766,"date":"2024-08-03T08:15:02","date_gmt":"2024-08-03T06:15:02","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1766"},"modified":"2024-08-03T08:15:05","modified_gmt":"2024-08-03T06:15:05","slug":"08-jerarquia-de-clases-y-metodos-abstractos-en-php","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/php\/poo-php\/08-jerarquia-de-clases-y-metodos-abstractos-en-php\/","title":{"rendered":"08. Jerarqu\u00eda de clases y m\u00e9todos abstractos en PHP"},"content":{"rendered":"\n<p>Como vimos en anteriores cap\u00edtulos, el concepto de&nbsp;<strong>abstracci\u00f3n<\/strong> es el de <strong>aislamiento de un elemento de su contexto<\/strong>, s\u00f3lo define las caracter\u00edsticas esenciales que un objeto debe tener. Una <strong>clase<\/strong>&nbsp;<strong>abstracta<\/strong>&nbsp;es <strong><em>aquella que no necesita ser instanciada, pero sin embargo, ser\u00e1n heredadas en alg\u00fan momento<\/em><\/strong>. La finalidad de las&nbsp;<strong>clases abstractas<\/strong>&nbsp;es declarar&nbsp;<strong>clases gen\u00e9ricas<\/strong>&nbsp;que necesitan ser declaradas pero a las cuales no se puede otorgar una definici\u00f3n precisa. Es decir,&nbsp;<strong>una clase abstracta es un modelo a seguir.<\/strong><\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Como tambi\u00e9n se dijo, es posible tambi\u00e9n declarar&nbsp;<strong>m\u00e9todos abstractos<\/strong>, los cuales no tienen cuerpo.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ejercicio<\/h2>\n\n\n\n<p>Vamos a hacer un ejercicio para entender todo el concepto de&nbsp;<strong>clases abstractas<\/strong>.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<p>Vamos a definir varios archivos para ver pol\u00edgonos. Los archivos que se crear\u00e1n por orden alfab\u00e9tico son:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>cuadrado.php<\/em><\/strong><\/li>\n\n\n\n<li><strong><em>figuras.php<\/em><\/strong><\/li>\n\n\n\n<li><strong><em>hexagono.php<\/em><\/strong><\/li>\n\n\n\n<li><strong><em>poligono.php<\/em><\/strong><\/li>\n\n\n\n<li><strong><em>rectangulo.php<\/em><\/strong><\/li>\n\n\n\n<li><strong><em>triangulo.php<\/em><\/strong><\/li>\n<\/ul>\n\n\n\n<p>El primer archivo es el archivo&nbsp;<strong><em>poligono.php<\/em><\/strong>&nbsp;que tiene la siguiente sintaxis (incluye una&nbsp;<strong>clase abstracta<\/strong>&nbsp;de la cual heredar\u00e1n las&nbsp;<strong>clases hijas<\/strong>).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\nabstract class Poligono {\n&nbsp; &nbsp; protected $lados;\n\n&nbsp; &nbsp; abstract protected function perimetro();\n\n&nbsp; &nbsp; abstract protected function area();\n\n&nbsp; &nbsp; public function lados() {\n&nbsp; &nbsp; &nbsp; &nbsp; return 'Un ' . get_called_class() . ' tiene &lt;mark&gt;' . $this-&gt;lados . '&lt;\/mark&gt; lados';\n&nbsp; &nbsp; }\n}\n?&gt;<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Nota<\/strong>: una <strong>clase abstracta<\/strong>, dentro de ella puede tener <strong>funciones p\u00fablicas<\/strong>, como es el caso de la \u00faltima funci\u00f3n de nuestra <strong>clase abstracta<\/strong> Poligono.<\/p>\n<\/blockquote>\n\n\n\n<p>Veamos la sintaxis de la p\u00e1gina&nbsp;<strong><em>triangulo.php<\/em><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\nclass Triangulo extends Poligono {\n&nbsp; &nbsp; private $lado_a;\n&nbsp; &nbsp; private $lado_b;\n&nbsp; &nbsp; private $lado_c;\n&nbsp; &nbsp; private $altura;\n\n&nbsp; &nbsp; public function __construct($a, $b, $c, $h) {\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;lados = 3;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;lado_a = $a;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;lado_b = $b;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;lado_c = $c;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;altura = $h;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function perimetro() {\n&nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;lado_a + $this-&gt;lado_b + $this-&gt;lado_c;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function area() {\n&nbsp; &nbsp; &nbsp; &nbsp; return ($this-&gt;lado_b * $this-&gt;altura) \/ 2;\n&nbsp; &nbsp; }\n}\n?&gt;<\/pre>\n\n\n\n<p>Veamos la sintaxis de la p\u00e1gina&nbsp;<strong><em>cuadrado.php<\/em><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\nclass Cuadrado extends Poligono {\n&nbsp; &nbsp; private $lado;\n&nbsp; &nbsp; private $altura;\n\n&nbsp; &nbsp; public function __construct($l) {\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;lados = 4;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;lado = $l;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function perimetro() {\n&nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;lados * $this-&gt;lado;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function area() {\n&nbsp; &nbsp; &nbsp; &nbsp; return pow($this-&gt;lado, 2);\n&nbsp; &nbsp; }\n}\n?&gt;<\/pre>\n\n\n\n<p>Veamos la sintaxis del archivo&nbsp;<strong><em>rectangulo.php<\/em><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\nclass Rectangulo extends Poligono {\n&nbsp; &nbsp; private $base;\n&nbsp; &nbsp; private $altura;\n\n&nbsp; &nbsp; public function __construct($b, $h) {\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;lados = 4;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;base = $b;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;altura = $h;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function perimetro() {\n&nbsp; &nbsp; &nbsp; &nbsp; return ($this-&gt;base + $this-&gt;altura) * 2;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function area() {\n&nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;base * $this-&gt;altura;\n&nbsp; &nbsp; }\n}\n?&gt;<\/pre>\n\n\n\n<p>Veamos la sintaxis del archivo&nbsp;<strong><em>hexagono.php<\/em><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\nclass Hexagono extends Poligono {\n&nbsp; &nbsp; private $lado;\n&nbsp; &nbsp; private $apotema;\n\n&nbsp; &nbsp; public function __construct($l, $a) {\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;lados = 6;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;lado = $l;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;apotema = $a;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function perimetro() {\n&nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;lados * $this-&gt;lado;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function area() {\n&nbsp; &nbsp; &nbsp; &nbsp; return (self::perimetro() * $this-&gt;apotema) \/ 2;\n&nbsp; &nbsp; }\n}\n?&gt;<\/pre>\n\n\n\n<p>Y por \u00faltimo, el archivo&nbsp;<strong><em>figuras.php<\/em><\/strong>&nbsp;que es donde vamos a imprimir todo.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\nrequire(\"poligono.php\");\nrequire(\"triangulo.php\");\nrequire(\"cuadrado.php\");\nrequire(\"rectangulo.php\");\nrequire(\"hexagono.php\");\n\necho \"&lt;h1&gt;Pol\u00edgonos&lt;\/h1&gt;\n&lt;p&gt;Figura geom\u00e9trica plana la cual est\u00e1 limitada por 3 o m\u00e1s rectas\ny tiene 3 o m\u00e1s \u00e1ngulos y v\u00e9rtices&lt;\/p&gt;\n\n&lt;h2&gt;Per\u00edmetro&lt;\/h2&gt;\n&lt;p&gt;El per\u00edmetro de un pol\u00edgono es igual a la suma de las longitudes\nde sus lados&lt;\/p&gt;\n&lt;h2&gt;\u00c1rea&lt;\/h2&gt;\n&lt;p&gt;El \u00e1rea de un pol\u00edgono es la medida de la regi\u00f3n o superficie\nencerrada por un pol\u00edgono&lt;\/p&gt;\n&lt;hr \/&gt;\n\";\n\necho \"&lt;h2&gt;Tri\u00e1ngulo&lt;\/h2&gt;\";\n$triangulo = new Triangulo(3,6,9,10);\necho '&lt;p&gt;' . $triangulo-&gt;lados() . '&lt;\/p&gt;';\necho '&lt;p&gt;Per\u00edmetro del ' . get_class($triangulo) . ': &lt;mark&gt;' . $triangulo-&gt;perimetro() . '&lt;\/mark&gt;&lt;\/p&gt;';\necho '&lt;p&gt;\u00c1rea del ' . get_class($triangulo) . ': &lt;mark&gt;' . $triangulo-&gt;area() . '&lt;\/mark&gt;&lt;\/p&gt;';\necho '&lt;hr \/&gt;';\n\necho '&lt;h2&gt;Cuadrado&lt;\/h2&gt;';\n$cuadrado = new Cuadrado(8);\necho '&lt;p&gt;' . $cuadrado-&gt;lados() . '&lt;\/p&gt;';\necho '&lt;p&gt;Per\u00edmetro del ' . get_class($cuadrado) . ': &lt;mark&gt;' . $cuadrado-&gt;perimetro() . '&lt;\/mark&gt;&lt;\/p&gt;';\necho '&lt;p&gt;\u00c1rea del ' . get_class($cuadrado) . ': &lt;mark&gt;' . $cuadrado-&gt;area() . '&lt;\/mark&gt;&lt;\/p&gt;';\necho '&lt;hr \/&gt;';\n\necho '&lt;h2&gt;Rect\u00e1ngulo&lt;\/h2&gt;';\n$rectangulo = new Rectangulo(7,4);\necho '&lt;p&gt;' . $rectangulo-&gt;lados() . '&lt;\/p&gt;';\necho '&lt;p&gt;Per\u00edmetro del ' . get_class($rectangulo) . ': &lt;mark&gt;' . $rectangulo-&gt;perimetro() . '&lt;\/mark&gt;&lt;\/p&gt;';\necho '&lt;p&gt;\u00c1rea del ' . get_class($rectangulo) . ': &lt;mark&gt;' . $rectangulo-&gt;area() . '&lt;\/mark&gt;&lt;\/p&gt;';\necho '&lt;hr \/&gt;';\n\necho '&lt;h2&gt;Hex\u00e1gono&lt;\/h2&gt;';\n$hexagono = new Hexagono(8,9);\necho '&lt;p&gt;' . $hexagono-&gt;lados() . '&lt;\/p&gt;';\necho '&lt;p&gt;Per\u00edmetro del ' . get_class($hexagono) . ': &lt;mark&gt;' . $hexagono-&gt;perimetro() . '&lt;\/mark&gt;&lt;\/p&gt;';\necho '&lt;p&gt;\u00c1rea del ' . get_class($hexagono) . ': &lt;mark&gt;' . $hexagono-&gt;area() . '&lt;\/mark&gt;&lt;\/p&gt;';\necho '&lt;hr \/&gt;';\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Como vimos en anteriores cap\u00edtulos, el concepto de&nbsp;abstracci\u00f3n es el de aislamiento de un elemento de su contexto, s\u00f3lo define [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1735,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-1766","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 vimos en anteriores cap\u00edtulos, el concepto de&nbsp;abstracci\u00f3n es el de aislamiento de un elemento de su contexto, s\u00f3lo define [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1766","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=1766"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1766\/revisions"}],"predecessor-version":[{"id":1768,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1766\/revisions\/1768"}],"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=1766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}