{"id":86653,"date":"2017-06-17T15:21:45","date_gmt":"2017-06-17T15:21:45","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=86653"},"modified":"2017-06-17T15:21:45","modified_gmt":"2017-06-17T15:21:45","slug":"namespaces-in-php","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/namespaces-in-php\/","title":{"rendered":"Namespaces in PHP"},"content":{"rendered":"<p>Today we are going to learn about Namespace in PHP. \u00a0Let&#8217;s start with definition of namespace So &#8220;Namespace are basically a way of organising your PHP classes and preventing from any kind of code conflicts . See this example ,<\/p>\n<pre class=\"brush:php\">&lt;?php \r\nnamespace Webkul;\r\n\r\n\/\/We have here a namespace with name Webkul and a class Name Opencart\r\n\r\nclass Opencart {\r\n\r\n\/\/Your all code will be here for this class\r\n\r\n}<\/pre>\n<p>In this example we have \u00a0created a \u00a0class called <em>Opencart<\/em>\u00a0inside of the <em>Webkul<\/em>\u00a0namespace. If we did not create namespace for class and \u00a0we have another\u00a0class with the \u00a0same name of &#8216;Opencart&#8217; included in this file \u00a0we would get an error stating we cannot re-declare class. Namespace will allow us to create two class name with same name and we can include both in same file.So we can\u00a0create another class with same name \u00a0like this-<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\nnamespace Demo;\r\n\/\/We have here a namespace with name Demo\u00a0and a class Name Opencart \r\nclass Opencart {\r\n\/\/Your all code will be here for this class\r\n}<\/pre>\n<p>Here we have created same class Opencart in another namespace Demo .Now we can include both file in our application and easily we can differentiate and use \u00a0both class Opencart from both namespace. As i have given a example how we can use both class in same file<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\n\/\/ include both of our Opencart classes \r\nrequire \"Demo\/Opencart.php\";\r\nrequire \"Webkul\/Opencart.php\"; \r\n\r\n\/\/ create a new instance for Opencart in the Demo namespace\r\n $Opencart1 = new Demo\\Opencart(); \r\n \/\/ create a new instance for Opencart in the Webkul namespace \r\n $Opencart2 = new Webkul\\Opencart();<\/pre>\n<p>Both of these classes are different from each other and also \u00a0have different code , different\u00a0functionality . But here we are using both and namespace allow us to \u00a0use same class name and differentiate them by there namespaces. Also we can make these code more readable by using PHP use function.\u00a0For example, let&#8217;s say \u00a0we want to just use <em>Opencart<\/em>\u00a0in place of \u00a0Demo<em>\\Opencart<\/em>. Then by using PHP use function we can do like this<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\n require \"Demo\/Opencart.php\";\r\n require \"Webkul\/Opencart.php\"; \r\n\/\/ included both of our Opencart classes \r\n\r\nuse Demo\\Opencart as Opencart;\r\n\/\/this is to tell our code that i will use Opencart in place of Demo\/Opencart\r\n\r\n\/\/Now use of Opencart for Demo namespace\r\n $Opencart1 = new Opencart();<\/pre>\n<p>We have used use PHP function to tell our code that i want use Opencart in place of writing Demo\/Opencart. Now also we can use another namespace class for that \u00a0we have to do like this .<\/p>\n<pre class=\"brush:php\">\/\/This will change the Opencart class from demo to webkul namespace\r\n\r\nuse Webkul\\Opencart as Opencart;\r\n\/\/ Now if you will create a instance for opencart class\r\n$Opencart2 = new Opencart(); \r\n\r\n\/\/This is new intance of class Opencart from Webkul namespace\r\n<\/pre>\n<p>Now if you will use Opencart class then you are using it form Webkul namespace . and you can create instance which will belongs to Opencart class from Webkul namespace. Thats all from this blog.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we are going to learn about Namespace in PHP. \u00a0Let&#8217;s start with definition of namespace So &#8220;Namespace are basically a way of organising your PHP classes and preventing from any kind of code conflicts . See this example , &lt;?php namespace Webkul; \/\/We have here a namespace with name Webkul and a class Name <a href=\"https:\/\/webkul.com\/blog\/namespaces-in-php\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":148,"featured_media":84482,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[305,13],"tags":[],"class_list":["post-86653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-opencart","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Namespaces in PHP - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/namespaces-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Namespaces in PHP - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Today we are going to learn about Namespace in PHP. \u00a0Let&#8217;s start with definition of namespace So &#8220;Namespace are basically a way of organising your PHP classes and preventing from any kind of code conflicts . See this example , &lt;?php namespace Webkul; \/\/We have here a namespace with name Webkul and a class Name [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/namespaces-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-06-17T15:21:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\" \/>\n\t<meta property=\"og:image:width\" content=\"825\" \/>\n\t<meta property=\"og:image:height\" content=\"260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Mangesh Yadav\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mangesh Yadav\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/namespaces-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/namespaces-in-php\/\"},\"author\":{\"name\":\"Mangesh Yadav\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ed1951d709bbfceb1fbf53ae003eac19\"},\"headline\":\"Namespaces in PHP\",\"datePublished\":\"2017-06-17T15:21:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/namespaces-in-php\/\"},\"wordCount\":318,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/namespaces-in-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"articleSection\":[\"opencart\",\"php\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/namespaces-in-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/namespaces-in-php\/\",\"url\":\"https:\/\/webkul.com\/blog\/namespaces-in-php\/\",\"name\":\"Namespaces in PHP - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/namespaces-in-php\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/namespaces-in-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"datePublished\":\"2017-06-17T15:21:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/namespaces-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/namespaces-in-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/namespaces-in-php\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"width\":\"825\",\"height\":\"260\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/namespaces-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Namespaces in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ed1951d709bbfceb1fbf53ae003eac19\",\"name\":\"Mangesh Yadav\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/de88cb7793914e715061efdd2f175586c8f16df48f6cf0cf9b12751abf72b1e2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/de88cb7793914e715061efdd2f175586c8f16df48f6cf0cf9b12751abf72b1e2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Mangesh Yadav\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/mangesh-yadav079\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Namespaces in PHP - Webkul Blog","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:\/\/webkul.com\/blog\/namespaces-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Namespaces in PHP - Webkul Blog","og_description":"Today we are going to learn about Namespace in PHP. \u00a0Let&#8217;s start with definition of namespace So &#8220;Namespace are basically a way of organising your PHP classes and preventing from any kind of code conflicts . See this example , &lt;?php namespace Webkul; \/\/We have here a namespace with name Webkul and a class Name [...]","og_url":"https:\/\/webkul.com\/blog\/namespaces-in-php\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2017-06-17T15:21:45+00:00","og_image":[{"width":825,"height":260,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","type":"image\/png"}],"author":"Mangesh Yadav","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Mangesh Yadav","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/namespaces-in-php\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/namespaces-in-php\/"},"author":{"name":"Mangesh Yadav","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ed1951d709bbfceb1fbf53ae003eac19"},"headline":"Namespaces in PHP","datePublished":"2017-06-17T15:21:45+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/namespaces-in-php\/"},"wordCount":318,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/namespaces-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","articleSection":["opencart","php"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/namespaces-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/namespaces-in-php\/","url":"https:\/\/webkul.com\/blog\/namespaces-in-php\/","name":"Namespaces in PHP - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/namespaces-in-php\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/namespaces-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","datePublished":"2017-06-17T15:21:45+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/namespaces-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/namespaces-in-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/namespaces-in-php\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","width":"825","height":"260"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/namespaces-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Namespaces in PHP"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ed1951d709bbfceb1fbf53ae003eac19","name":"Mangesh Yadav","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/de88cb7793914e715061efdd2f175586c8f16df48f6cf0cf9b12751abf72b1e2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/de88cb7793914e715061efdd2f175586c8f16df48f6cf0cf9b12751abf72b1e2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Mangesh Yadav"},"url":"https:\/\/webkul.com\/blog\/author\/mangesh-yadav079\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/86653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/148"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=86653"}],"version-history":[{"count":1,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/86653\/revisions"}],"predecessor-version":[{"id":86662,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/86653\/revisions\/86662"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media\/84482"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=86653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=86653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=86653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}