{"id":311,"date":"2014-08-12T01:08:49","date_gmt":"2014-08-12T09:08:49","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=311"},"modified":"2014-08-12T01:08:49","modified_gmt":"2014-08-12T09:08:49","slug":"explain-php-namespaces","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=311","title":{"rendered":"Explain PHP namespaces"},"content":{"rendered":"<p><a href=\"http:\/\/daylerees.com\/php-namespaces-explained\">http:\/\/daylerees.com\/php-namespaces-explained<\/a><\/p>\n<p>Previously in PHP you can\u2019t have two classes that share the same name. They have to be unique. The issue with this restriction is that if you are using a third party library which has a class named User, then you can\u2019t create your own class also called User. This is a real shame, because that\u2019s a pretty convenient class name right?<\/p>\n<p>PHP namespaces allow us to circumvent this issue, in fact we can have as many User classes as we like. Not only that, but we can use namespaces to contain our similar code into neat little packages, or even to show ownership.<\/p>\n<p><code><?php\n\nnamespace Stark;\n\n\/\/ app\/models\/another.php\n\nclass Eddard\n{\n\n} ?><\/code><\/p>\n<p>Using a class declared in a namespace <\/p>\n<p><code><?php\n\n\/\/ app\/routes.php\n\n$eddard = new Stark\\Eddard(); ?><\/code><\/p>\n<p>Classes in Global namespace can also be referred as : <\/p>\n<p>$eddard = new \\Eddard();<\/p>\n<p>Namespaces can have as many levels of hierarchy as they need to.<\/p>\n<p>This\\Namespace\\And\\Class\\Combination\\Is\\Silly\\But\\Works<\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/daylerees.com\/php-namespaces-explained Previously in PHP you can\u2019t have two classes that share the same name. They have to be unique. The issue with this restriction is that if you are using a third party library which has a class named User, then you can\u2019t create your own class also called User. This is a real shame, &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=311\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Explain PHP namespaces<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-311","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=311"}],"version-history":[{"count":1,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":313,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/311\/revisions\/313"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}