{"id":1804,"date":"2024-08-03T08:25:46","date_gmt":"2024-08-03T06:25:46","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1804"},"modified":"2024-08-03T08:25:47","modified_gmt":"2024-08-03T06:25:47","slug":"19-conexion-a-mysql-con-clase-abstracta-php","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/php\/poo-php\/19-conexion-a-mysql-con-clase-abstracta-php\/","title":{"rendered":"19. Conexi\u00f3n a MySQL con clase abstracta PHP"},"content":{"rendered":"\n<p>En este cap\u00edtulo vamos a crear un <strong>modelo abstracto general<\/strong>, el modelo padre que nos permitir\u00e1 la conexi\u00f3n a la base de datos <strong>MySQL<\/strong>. El paradigma que estamos utilizando es el <strong>POO<\/strong>, por lo que no vamos a utilizar <strong>programaci\u00f3n estructurada<\/strong> ni <strong>funcional<\/strong>, que tambi\u00e9n disponemos en <strong>PHP<\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Vamos a crear una <strong>clase abstracta<\/strong> que permite conectarnos y ejecutar <strong>queries<\/strong> a nuestra <strong>base de datos<\/strong>. Vamos a crear un archivo denominado <em>Model.php<\/em>. Como vemos, el archivo comienza en MAY ya que vamos a trabajar con <strong>clases<\/strong>, por lo que el formato de sintaxis es <strong>CamelCase<\/strong>.<\/p>\n\n\n\n<p>Hay que recordar de cap\u00edtulos anteriores, que las <strong>clases abstractas<\/strong> pueden tener <strong>m\u00e9todos abstractos<\/strong>, que significa que son <strong>m\u00e9todos<\/strong> que las <strong>clases<\/strong> que hereden de esta <strong>clase<\/strong> las van a heredar y se van a implementar en esas <strong>clases hijas<\/strong>. En nuestra <strong>clase abstracta<\/strong> se definen s\u00f3lo los nombres de esos <strong>m\u00e9todos<\/strong>.<\/p>\n\n\n\n<p>La sintaxis de nuestro archivo <em>Model.php<\/em> ser\u00e1 la siguiente.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\nabstract class Model {\n&nbsp; &nbsp; \/\/ Atributos\n&nbsp; &nbsp; private static $db_host = 'localhost';\n&nbsp; &nbsp; private static $db_user = 'root';\n&nbsp; &nbsp; private static $db_passwd = 'root';\n&nbsp; &nbsp; protected $db_name;\n&nbsp; &nbsp; private static $db_charset = 'utf8';\n&nbsp; &nbsp; private $conn;\n&nbsp; &nbsp; protected $query;\n&nbsp; &nbsp; protected $rows = array();\n\n&nbsp; &nbsp; \/\/ M\u00e9todos abstractos para CRUD de clases que hereden\n&nbsp; &nbsp; abstract protected function create();\n&nbsp; &nbsp; abstract protected function read();\n&nbsp; &nbsp; abstract protected function update();\n&nbsp; &nbsp; abstract protected function delete();\n\n&nbsp; &nbsp; \/\/ M\u00e9todo privado para conectarse a la base de datos\n&nbsp; &nbsp; private function db_open() {\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;conn = new mysqli(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self::$db_host,\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self::$db_user,\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self::$db_passwd,\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;db_name\n&nbsp; &nbsp; &nbsp; &nbsp; );\n\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;conn-&gt;set_charset(self::$db_charset);\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; \/\/ M\u00e9todo privado para desconectarse de la base de datos\n&nbsp; &nbsp; private function db_close() {\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;conn-&gt;close();\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; \/\/ Establecer un QUERY simple del tipo INSERT, DELETE, UPDATE\n&nbsp; &nbsp; protected function set_query() {\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;db_open();\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;conn-&gt;query($this-&gt;query);\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;db_close();\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; \/\/ Traer resultados de una consulta tipo SELECT en un array\n&nbsp; &nbsp; protected function get_query() {\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;db_open();\n&nbsp; &nbsp; &nbsp; &nbsp; $result = $this-&gt;conn-&gt;query($this-&gt;query);\n&nbsp; &nbsp; &nbsp; &nbsp; while( $this-&gt;rows[] = $result-&gt;fetch_assoc() );\n&nbsp; &nbsp; &nbsp; &nbsp; $result-&gt;close();\n&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;db_close();\n&nbsp; &nbsp; &nbsp; &nbsp; return array_pop($this-&gt;rows);\n&nbsp; &nbsp; }\n}\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo vamos a crear un modelo abstracto general, el modelo padre que nos permitir\u00e1 la conexi\u00f3n a la [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1735,"menu_order":18,"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-1804","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":"En este cap\u00edtulo vamos a crear un modelo abstracto general, el modelo padre que nos permitir\u00e1 la conexi\u00f3n a la [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1804","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=1804"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1804\/revisions"}],"predecessor-version":[{"id":1806,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1804\/revisions\/1806"}],"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=1804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}