{"id":1758,"date":"2024-08-02T11:54:34","date_gmt":"2024-08-02T09:54:34","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1758"},"modified":"2024-08-02T11:54:35","modified_gmt":"2024-08-02T09:54:35","slug":"06-clases-y-objetos-en-php","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/php\/poo-php\/06-clases-y-objetos-en-php\/","title":{"rendered":"06. Clases y objetos en PHP"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Recordatorio de modificadores de acceso<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Como dijimos en el cap\u00edtulo anterior, los&nbsp;<strong>modificadores de acceso<\/strong>&nbsp;pueden ser:<\/p>\n\n\n\n<!--more-->\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>public<\/strong>: acceso desde cualquier\u00a0<strong>m\u00e9todo<\/strong>\u00a0de la\u00a0<strong>clase<\/strong>\u00a0u\u00a0<strong>objeto<\/strong>\u00a0que lo invoque.<\/li>\n\n\n\n<li><strong>private<\/strong>: acceso s\u00f3lo desde los\u00a0<strong>m\u00e9todos<\/strong>\u00a0de la\u00a0<strong>clase<\/strong>, los\u00a0<strong>objetos<\/strong>\u00a0no los pueden invocar.<\/li>\n\n\n\n<li><strong>protected<\/strong>: acceso s\u00f3lo desde los\u00a0<strong>m\u00e9todos<\/strong>\u00a0de la\u00a0<strong>clase<\/strong>\u00a0y\u00a0<strong>subclases<\/strong>\u00a0que la hereden, los\u00a0<strong>objetos<\/strong>\u00a0no los pueden invocar.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">M\u00e9todos m\u00e1gicos<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Tambi\u00e9n en el cap\u00edtulo anterior habl\u00e1bamos de lo que eran los&nbsp;<strong>m\u00e9todos m\u00e1gicos<\/strong>, que son el&nbsp;<strong>constructor<\/strong>&nbsp;y el&nbsp;<strong>destructor<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>M\u00e9todo constructor<\/strong>: es aquel\u00a0<strong>m\u00e9todo<\/strong>\u00a0que se ejecuta autom\u00e1ticamente al inicio de\u00a0<strong>instanciar<\/strong>\u00a0la\u00a0<strong>clase<\/strong>.<\/li>\n\n\n\n<li><strong>M\u00e9todo destructor<\/strong>:\u00a0<strong>m\u00e9todo<\/strong>\u00a0que se ejecuta autom\u00e1ticamente al final de\u00a0<strong>instanciar<\/strong>\u00a0la\u00a0<strong>clase<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Ejm<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Veamos la sintaxis del cap\u00edtulo anterior en un ejercicio pr\u00e1ctico.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\nclass Persona {\n&nbsp; &nbsp; \/\/ ATRIBUTOS\n&nbsp; &nbsp; public $nombre;\n&nbsp; &nbsp; public $apellido;\n&nbsp; &nbsp; public $edad;\n&nbsp; &nbsp; public $sexo;\n&nbsp; &nbsp; private $dni;\n\n    \/\/ ATRIBUTO EST\u00c1TICO\n&nbsp; &nbsp; public static $manos = ' 5 dedos en cada mano';\n\n    \/\/ CONSTANTE\n&nbsp; &nbsp; const MEJOR_CUALIDAD = ' poder pensar';\n\n&nbsp; &nbsp; \/\/ M\u00c9TODOS CONSTRUCTOR Y DESTRUCTOR\n&nbsp; &nbsp; public function __construct($n, $a, $e, $s) {\n&nbsp; &nbsp; &nbsp; &nbsp; echo \"&lt;p&gt;&lt;mark&gt;Soy el constructor de la clase&lt;\/mark&gt;&lt;\/p&gt;\";\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;nombre = $n;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;apellido = $a;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;edad = $e;\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;sexo = ($s) ? 'Macho' : 'Hembra';\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function __destruct() {\n&nbsp; &nbsp; &nbsp; &nbsp; echo \"&lt;p&gt;&lt;mark&gt;Soy el destructor de la clase&lt;\/mark&gt;&lt;\/p&gt;\";\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; \/\/ M\u00c9TODOS DE LA CLASE\n&nbsp; &nbsp; public function saludar() {\n&nbsp; &nbsp; &nbsp; &nbsp; echo '&lt;p&gt;Hola, mi nombre es ' . $this-&gt;nombre . '&lt;\/p&gt;';\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function comer($comida) {\n&nbsp; &nbsp; &nbsp; &nbsp; echo '&lt;p&gt;Hoy toca comer' . $this-&gt;comida . '&lt;\/p&gt;';\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public function mas_info() {\n&nbsp; &nbsp; &nbsp; &nbsp;\n        \/\/ LLAMANDO A M\u00c9TODOS\n        $this-&gt;saludar();\n&nbsp; &nbsp; &nbsp; &nbsp; self::saludar();\n&nbsp; &nbsp; &nbsp; &nbsp; Persona::saludar();\n\n        \/\/ LLAMANDO A ATRIBUTOS CON self Y LA PROPIA CLASE\n&nbsp; &nbsp; &nbsp; &nbsp; echo '&lt;p&gt;El ser humano tiene dos manos con' . self::$manos . '&lt;\/p&gt;';\n  &nbsp; &nbsp; &nbsp; echo '&lt;p&gt;La mejor cualidad de un ser humano es' . Persona::MEJOR_CUALIDAD . '&lt;\/p&gt;';\n&nbsp; &nbsp; }\n}\n\n\/\/ HACEMOS UNA INSTANCIA A LA CLASE Persona\n$francisco = new Persona('Francisco', 'Paredes', 51, true);\n\n\/\/ IMPRIMIMOS ALGUNOS ATRIBUTOS\necho '&lt;p&gt;Mi nombre es ' . $francisco-&gt;nombre . '&lt;\/p&gt;';\necho '&lt;p&gt;Mi apellido es ' . $francisco-&gt;apellido . '&lt;\/p&gt;';\necho '&lt;p&gt;Tengo ' . $francisco-&gt;edad . ' a\u00f1os&lt;\/p&gt;';\n\n\/\/ IMPRIMIMOS ALG\u00daN M\u00c9TODO\n$francisco-&gt;saludar();\n$francisco-&gt;mas_info();\n?&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Funci\u00f3n var_dump()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Existe una funci\u00f3n denominada <strong><em>var_dump()<\/em><\/strong> que nos permite analizar la <strong>estructura de un objeto<\/strong>. Veamos un ejm.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var_dump($francisco);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recordatorio de modificadores de acceso Como dijimos en el cap\u00edtulo anterior, los&nbsp;modificadores de acceso&nbsp;pueden ser:<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1735,"menu_order":5,"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-1758","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":"Recordatorio de modificadores de acceso Como dijimos en el cap\u00edtulo anterior, los&nbsp;modificadores de acceso&nbsp;pueden ser:","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1758","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=1758"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1758\/revisions"}],"predecessor-version":[{"id":1760,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1758\/revisions\/1760"}],"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=1758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}