{"id":1577,"date":"2021-06-01T04:24:06","date_gmt":"2021-06-01T04:24:06","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=1577"},"modified":"2021-06-01T05:18:42","modified_gmt":"2021-06-01T05:18:42","slug":"namespaces","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/php\/namespaces\/","title":{"rendered":"Namespaces"},"content":{"rendered":"\n<h3>What are namespaces?<\/h3>\n\n\n\n<p>Namespaces are a way of encapsulating items.<br>e.g. In any operating system directories serve to group related files, and act as a namespace for the files within them.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/06\/image.png\"><img loading=\"lazy\" width=\"160\" height=\"177\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/06\/image.png\" alt=\"\" class=\"wp-image-1578\"\/><\/a><figcaption>foo file in abc lmn pqr and xyz directory<\/figcaption><\/figure>\n\n\n\n<p>As a concrete example, the file\u00a0<code>foo.txt<\/code>\u00a0can exist in\u00a0all 4 directories with different contents in each.<\/p>\n\n\n\n<p>Similarly to avoid ambiguity in PHP we have namespaces. Namespaces solve the confusion of having same class name in program.<\/p>\n\n\n\n<p>Let&#8217;s understand this with example.<\/p>\n\n\n\n<p>AdminAccount.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nclass AdminAccount {\n        public function __construct() {\n                echo \"Admin Account Controller...\" . PHP_EOL;\n        }\n}<\/code><\/pre>\n\n\n\n<p>UserAccount.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\nclass UserAccount {\r\n        public function __construct() {\r\n                echo \"User Account Controller...\" . PHP_EOL;\r\n        }\r\n}<\/code><\/pre>\n\n\n\n<p>index.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nrequire(\"UserAccount.php\");\nrequire(\"AdminAccount.php\");\n\nnew UserAccount();\nnew AdminAccount();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Now, what if we have the same class name in both files. <br>This scenario always comes when you use third-party libraries or any framework.<\/p>\n\n\n\n<p>AdminAccount.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\nclass Account {\r\n        public function __construct() {\r\n                echo \"Admin Account Controller...\" . PHP_EOL;\r\n        }\r\n}<\/code><\/pre>\n\n\n\n<p>UserAccount.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nclass Account {\n        public function __construct() {\n                echo \"User Account Controller...\" . PHP_EOL;\n        }\n}<\/code><\/pre>\n\n\n\n<p>index.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nrequire(\"UserAccount.php\");\nrequire(\"AdminAccount.php\");\n\nnew Account();\nnew Account();<\/code><\/pre>\n\n\n\n<p>This will create confusion about which instance to be created. <br>Now let&#8217;s resolve this problem using namespaces.<\/p>\n\n\n\n<p>AdminAccount.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n<strong>namespace Admin;<\/strong>\n\nclass Account {\n        public function __construct() {\n                echo \"Admin Account Controller...\" . PHP_EOL;\n        }\n}<\/code><\/pre>\n\n\n\n<p>UserAccount.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n<strong>namespace User;<\/strong>\n\nclass Account {\n        public function __construct() {\n                echo \"User Account Controller...\" . PHP_EOL;\n        }\n}<\/code><\/pre>\n\n\n\n<p>index.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\nrequire(\"UserAccount.php\");\r\nrequire(\"AdminAccount.php\");\r\n\r\nnew <strong>User\\<\/strong>Account();\r\nnew <strong>Admin\\<\/strong>Account();<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/06\/image-2.png\"><img loading=\"lazy\" width=\"593\" height=\"61\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/06\/image-2.png\" alt=\"\" class=\"wp-image-1592\" srcset=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/06\/image-2.png 593w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/06\/image-2-300x31.png 300w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/a><figcaption>script output<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/06\/image-1.png\"><img loading=\"lazy\" width=\"194\" height=\"83\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/06\/image-1.png\" alt=\"\" class=\"wp-image-1582\"\/><\/a><figcaption>directory structure<\/figcaption><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What are namespaces? Namespaces are a way of encapsulating items.e.g. In any operating system directories serve to group related files, and act as a namespace for the files within them. As a concrete example, the file\u00a0foo.txt\u00a0can exist in\u00a0all 4 directories with different contents in each. Similarly to avoid ambiguity in PHP we have namespaces. Namespaces [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1577"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=1577"}],"version-history":[{"count":9,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1577\/revisions"}],"predecessor-version":[{"id":1594,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1577\/revisions\/1594"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=1577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=1577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=1577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}