{"id":5239,"date":"2026-01-04T20:16:36","date_gmt":"2026-01-04T19:16:36","guid":{"rendered":"https:\/\/sutilweb.eu\/?page_id=5239"},"modified":"2026-01-04T20:16:37","modified_gmt":"2026-01-04T19:16:37","slug":"27-namespace-en-php","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/php\/php-practico\/27-namespace-en-php\/","title":{"rendered":"27. Namespace en PHP"},"content":{"rendered":"\n<p>A medida que nuestro programa va creciendo es muy probable que queramos organizarlo, es decir, crear carpetas y subcarpetas e ir dividiendo nuestros archivos en diferentes documentos. Para ello sirven los <strong>namespaces.<\/strong><\/p>\n\n\n\n<p>Vamos a crear un ejm en el cual hay creadas dos carpetas, <em>Carpeta1<\/em> y <em>Carpeta2.<\/em> Dentro de Carpeta1 tenemos el archivo PHP <em>Humano.php<\/em>, que tiene la siguiente sintaxis:<\/p>\n\n\n\n<p><em>Humano.php<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n  namespace Carpeta1;\n\n  class Humano {\n    public function saludar(){\n      echo \"Hola desde la clase Humano dentro de la carpeta carpeta1\";\n    }\n  }<\/code><\/pre>\n\n\n\n<p>Y dentro de&nbsp;<em>Carpeta2<\/em> tenemos el archivo con el mismo nombre, <em>Humano.php<\/em>, veamos su sintaxis<\/p>\n\n\n\n<p><em>Humano.php<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n&lt;?php\n\n  namespace Carpeta2;\n\n  class Humano {\n    public function saludar(){\n      echo \"Hola desde la clase Humano dentro de la carpeta carpeta2\";\n    }\n  }<\/code><\/pre>\n\n\n\n<p>Paralelo a las carpetas <em>Carpeta1<\/em> y <em>Carpeta2<\/em> tenemos un archivo <em>index.php<\/em> con la siguiente sintaxis.<\/p>\n\n\n\n<p><em>index.php<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n  require_once('Carpeta1\/Humano.php');\n  require_once('Carpeta2\/Humano.php');\n\n  $humano1 = new Carpeta1\\Humano;\n  $humano1->saludar();\n  echo \"&lt;br \/>\";\n  $humano2 = new Carpeta2\\Humano;\n  $humano2->saludar();<\/code><\/pre>\n\n\n\n<p>Como vemos, para <strong>instanciar<\/strong> a las clases del mismo nombre, <strong><em>Humano,<\/em><\/strong> se hace como se ve en el ejemplo, llamando a su namespace.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Utilizar un namespace por defecto<\/h2>\n\n\n\n<p>Para ello, en nuestro <em>index.php<\/em> utilizaremos la siguiente sintaxis.<\/p>\n\n\n\n<p><em>index.php<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n  \/\/ Esto indica que utilizaremos la carpeta Carpeta1\n  use Carpeta1\\Humano;\n\n  require_once('29-Carpeta1\/Humano.php');\n  require_once('29-Carpeta2\/Humano.php');\n\n  $humano1 = new Humano;\n  $humano1->saludar();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conflicto entre dos clases con el mismo nombre en dos carpetas distintas<\/h2>\n\n\n\n<p>En este caso lo que podemos hacer es darle distinto nombre mediante la keyword <strong><em>as<\/em><\/strong>, de la siguiente manera.<\/p>\n\n\n\n<p><em>index.php<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n&lt;?php\n\n  \/\/ Esto indica que utilizaremos la carpeta Carpeta1\n  use Carpeta1\\Humano;\n  use Carpeta2\\Humano as Humano2;\n\n  require_once('29-Carpeta1\/Humano.php');\n  require_once('29-Carpeta2\/Humano.php');\n\n  $humano1 = new Humano;\n  $humano1->saludar();\n\n  echo \"&lt;br \/>\";\n\n  $humano2 = new Humano2;\n  $humano2->saludar();<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A medida que nuestro programa va creciendo es muy probable que queramos organizarlo, es decir, crear carpetas y subcarpetas e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4518,"menu_order":26,"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-5239","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":"A medida que nuestro programa va creciendo es muy probable que queramos organizarlo, es decir, crear carpetas y subcarpetas e [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/5239","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=5239"}],"version-history":[{"count":7,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/5239\/revisions"}],"predecessor-version":[{"id":5252,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/5239\/revisions\/5252"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/4518"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}