{"id":35223,"date":"2019-07-10T11:50:43","date_gmt":"2019-07-10T11:50:43","guid":{"rendered":"https:\/\/www.knowband.com\/blog\/?p=35223\/"},"modified":"2019-07-10T17:20:43","modified_gmt":"2019-07-10T04:16:43","slug":"namespace-in-php","status":"publish","type":"post","link":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/","title":{"rendered":"Namespace in PHP"},"content":{"rendered":"<p><strong>Objective:<\/strong> To learn how we can use same name of the class in PHP using namespace.<\/p>\n<p>Generally we cannot use two or more class with the same name. It will give an error: &#8220;<strong>Cannot redeclare class<\/strong>&#8221; as shown below-<br \/>\n<a href=\"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2019\/07\/Picture2.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-35236 size-full\" src=\"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2019\/07\/Picture2.png\" alt=\"Namespace in PHP\" width=\"771\" height=\"168\" srcset=\"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2019\/07\/Picture2.png 771w, https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2019\/07\/Picture2-300x65.png 300w, https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2019\/07\/Picture2-768x167.png 768w\" sizes=\"auto, (max-width: 771px) 100vw, 771px\" \/><\/a><\/p>\n<p>To handle this, we can use the namespace as shown in the example given below:<\/p>\n<p>1. Defining the namespace Demo1 with Student class in the Demo1\/Student.php<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">&lt;?php\r\nnamespace Demo1;\r\nclass Student {\r\n\/\/All code will be here for this class\r\nvar $a = 10;\r\n}\r\n?&gt;<\/pre>\n<p>2. Defining the namespace Demo2 with Student class (class with same name) in the Demo2\/Student.php<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">&lt;?php\r\nnamespace Demo2;\r\nclass Student {\r\n\/\/All code will be here for this class\r\nvar $a = 20;\r\n}\r\n?&gt;<\/pre>\n<p>3. Now in the sample file index.php, we are able to use the both class without any error:<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">&lt;?php\r\n\/\/ including both of Student classes\r\nrequire &quot;Demo1\/Student.php&quot;;\r\nrequire &quot;Demo2\/Student.php&quot;;\r\n\r\n\/\/making an instance of Demo1\\Student()\r\n$ Student1 = new Demo1\\Student();\r\n\/\/using the first class functionality\r\necho $Student1-&gt;a.&#039;&lt;br&gt;&#039;;\r\n\r\n\/\/making another instance of Demo2\\Student()\r\n$Student2 = new Demo2\\Student();\r\n\/\/using the second class functionality\r\necho $Student2-&gt;a;<\/pre>\n<p><strong>Output Screen:<\/strong><\/p>\n<p><strong>10<\/strong><br \/>\n<strong> 20<\/strong><\/p>\n<p>Both classes will have different functionality and we will be able to use both classes in a single file easily.<\/p>\n<p><strong>Note:<\/strong> We can make the code more easy by using the PHP use() function:<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">use Demo1\\Student as Student1;\r\n$Student1= new Student1();\r\necho $Student1-&gt;a.&#039;&lt;br&gt;&#039;;\r\n\r\nuse Demo2\\Student as Student2;\r\n$Student2 = new Student2();\r\necho $Student2-&gt;a;<\/pre>\n<p>File Structure:<\/p>\n<p><strong>index.php<\/strong><br \/>\n<strong> Demo1\/Student.php<\/strong><br \/>\n<strong> Demo2\/Student.php<\/strong><\/p>\n<p>Related Article:\u00a0<strong><a href=\"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/\" target=\"_blank\">Auto Loading in PHP<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Objective: To learn how we can use same name of the class in PHP using namespace. Generally we cannot use two or more class with the same name. It will give an error: &#8220;Cannot redeclare class&#8221; as shown below- To handle this, we can use the namespace as shown in the example given below: 1. [&hellip;]<\/p>\n","protected":false},"author":27,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kbui_enabled":"","footnotes":""},"categories":[4],"tags":[14417,14416],"class_list":["post-35223","post","type-post","status-publish","format-standard","hentry","category-ecommerce-blog","tag-namespace-in-php","tag-php-namespace"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Namespace in PHP<\/title>\n<meta name=\"description\" content=\"Read the blog to learn how to use the same name of the class in PHP using namespace.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Namespace in PHP\" \/>\n<meta property=\"og:description\" content=\"Read the blog to learn how to use the same name of the class in PHP using namespace.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Knowband Blog | Ecommerce Modules\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Knowband\" \/>\n<meta property=\"article:published_time\" content=\"2019-07-10T11:50:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2019\/07\/Picture2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"771\" \/>\n\t<meta property=\"og:image:height\" content=\"168\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Akash Roshan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@KnowbandPlugin\" \/>\n<meta name=\"twitter:site\" content=\"@KnowbandPlugin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Akash Roshan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/\"},\"author\":{\"name\":\"Akash Roshan\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/person\\\/b6c3192deed20653bb1cbe0ab61c95e8\"},\"headline\":\"Namespace in PHP\",\"datePublished\":\"2019-07-10T11:50:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/\"},\"wordCount\":155,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/Picture2.png\",\"keywords\":[\"Namespace in PHP\",\"PHP namespace\"],\"articleSection\":[\"Ecommerce\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/\",\"name\":\"Namespace in PHP\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/Picture2.png\",\"datePublished\":\"2019-07-10T11:50:43+00:00\",\"description\":\"Read the blog to learn how to use the same name of the class in PHP using namespace.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/Picture2.png\",\"contentUrl\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/Picture2.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/namespace-in-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Namespace in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#website\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\",\"name\":\"Knowband Blog | Ecommerce Modules\",\"description\":\"Magento, Prestashop, Opencart, Shopify\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\",\"name\":\"Knowband Blog | Ecommerce Modules\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/09\\\/cropped-knowband_logo-v2.png\",\"contentUrl\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/09\\\/cropped-knowband_logo-v2.png\",\"width\":153,\"height\":61,\"caption\":\"Knowband Blog | Ecommerce Modules\"},\"image\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Knowband\",\"https:\\\/\\\/x.com\\\/KnowbandPlugin\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/person\\\/b6c3192deed20653bb1cbe0ab61c95e8\",\"name\":\"Akash Roshan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/plugins\\\/wp-user-avatar\\\/deprecated\\\/wp-user-avatar\\\/images\\\/wpua-96x96.png\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/plugins\\\/wp-user-avatar\\\/deprecated\\\/wp-user-avatar\\\/images\\\/wpua-96x96.png\",\"contentUrl\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/plugins\\\/wp-user-avatar\\\/deprecated\\\/wp-user-avatar\\\/images\\\/wpua-96x96.png\",\"caption\":\"Akash Roshan\"},\"description\":\"Akash is a PHP developer and wants to share his knowledge through Blogs. He is currently growing in the IT industry while fulfilling his own goals.\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/author\\\/aroshan\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Namespace in PHP","description":"Read the blog to learn how to use the same name of the class in PHP using namespace.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Namespace in PHP","og_description":"Read the blog to learn how to use the same name of the class in PHP using namespace.","og_url":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/","og_site_name":"Knowband Blog | Ecommerce Modules","article_publisher":"https:\/\/www.facebook.com\/Knowband","article_published_time":"2019-07-10T11:50:43+00:00","og_image":[{"width":771,"height":168,"url":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2019\/07\/Picture2.png","type":"image\/png"}],"author":"Akash Roshan","twitter_card":"summary_large_image","twitter_creator":"@KnowbandPlugin","twitter_site":"@KnowbandPlugin","twitter_misc":{"Written by":"Akash Roshan","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/#article","isPartOf":{"@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/"},"author":{"name":"Akash Roshan","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/person\/b6c3192deed20653bb1cbe0ab61c95e8"},"headline":"Namespace in PHP","datePublished":"2019-07-10T11:50:43+00:00","mainEntityOfPage":{"@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/"},"wordCount":155,"commentCount":0,"publisher":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization"},"image":{"@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2019\/07\/Picture2.png","keywords":["Namespace in PHP","PHP namespace"],"articleSection":["Ecommerce"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/","url":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/","name":"Namespace in PHP","isPartOf":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/#primaryimage"},"image":{"@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2019\/07\/Picture2.png","datePublished":"2019-07-10T11:50:43+00:00","description":"Read the blog to learn how to use the same name of the class in PHP using namespace.","breadcrumb":{"@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/#primaryimage","url":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2019\/07\/Picture2.png","contentUrl":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2019\/07\/Picture2.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.knowband.com\/blog\/fr\/"},{"@type":"ListItem","position":2,"name":"Namespace in PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.knowband.com\/blog\/fr\/#website","url":"https:\/\/www.knowband.com\/blog\/fr\/","name":"Knowband Blog | Ecommerce Modules","description":"Magento, Prestashop, Opencart, Shopify","publisher":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.knowband.com\/blog\/fr\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization","name":"Knowband Blog | Ecommerce Modules","url":"https:\/\/www.knowband.com\/blog\/fr\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/logo\/image\/","url":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2016\/09\/cropped-knowband_logo-v2.png","contentUrl":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2016\/09\/cropped-knowband_logo-v2.png","width":153,"height":61,"caption":"Knowband Blog | Ecommerce Modules"},"image":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Knowband","https:\/\/x.com\/KnowbandPlugin"]},{"@type":"Person","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/person\/b6c3192deed20653bb1cbe0ab61c95e8","name":"Akash Roshan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.knowband.com\/blog\/wp-content\/plugins\/wp-user-avatar\/deprecated\/wp-user-avatar\/images\/wpua-96x96.png","url":"https:\/\/www.knowband.com\/blog\/wp-content\/plugins\/wp-user-avatar\/deprecated\/wp-user-avatar\/images\/wpua-96x96.png","contentUrl":"https:\/\/www.knowband.com\/blog\/wp-content\/plugins\/wp-user-avatar\/deprecated\/wp-user-avatar\/images\/wpua-96x96.png","caption":"Akash Roshan"},"description":"Akash is a PHP developer and wants to share his knowledge through Blogs. He is currently growing in the IT industry while fulfilling his own goals.","url":"https:\/\/www.knowband.com\/blog\/author\/aroshan\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/35223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/comments?post=35223"}],"version-history":[{"count":4,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/35223\/revisions"}],"predecessor-version":[{"id":36449,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/35223\/revisions\/36449"}],"wp:attachment":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/media?parent=35223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/categories?post=35223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/tags?post=35223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}