{"id":1776,"date":"2012-07-10T20:33:10","date_gmt":"2012-07-10T19:33:10","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1776"},"modified":"2012-07-11T20:16:28","modified_gmt":"2012-07-11T19:16:28","slug":"php-namespace","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2012\/07\/10\/php-namespace\/","title":{"rendered":"PHP &#8211; namespace"},"content":{"rendered":"<p><a title=\"Namespace\" href=\"http:\/\/www.php.net\/manual\/en\/language.namespaces.rationale.php\" target=\"_blank\">Namespace<\/a>&#8216;ing is a great way of splitting functions \/ classes into different spaces that could have the same name but do not cause issues.  This is great if you are trying to combine different projects together that many have the same function names and you want to call different projects code functions without having to worry that you are calling the wrong function.<\/p>\n<p>For example if you have a file called codingfriends.php<\/p>\n<pre lang=\"php\">\r\n<?php\r\nnamespace codingfriends;\r\n\r\nfunction sayHello()\r\n{\r\n    echo \"Say hello from codingfriends.\";    \r\n}\r\n\r\nfunction sayHelloFromSub()\r\n{\r\n    echo sub\\sayHello();\r\n}\r\n?>\r\n<\/pre>\n<p>and another file called codingfriendssub.php<\/p>\n<pre lang=\"php\">\r\n<?php\r\nnamespace codingfriends\\sub;\r\n\r\nfunction sayHello()\r\n{\r\n    echo \"Say hello from codingfriends sub module\";    \r\n}\r\n?>\r\n<\/pre>\n<p>and then have the index.php file<\/p>\n<pre lang=\"php\">\r\n\r\n<?\r\ninclude \"codingfriends.php\";\r\ninclude \"codingfriends.sub.php\";\r\n\r\necho codingfriends\\sayHello().\"<br\/>\"; \/\/ calling the codingfriends namespace\r\necho codingfriends\\sub\\sayHello().\"<br\/>\"; \/\/ call the codingfriends sub namespace\r\necho codingfriends\\sayHelloFromSub().\"<br\/>\"; \/\/ calling the sub namespace quicker.\r\n?>\r\n<\/pre>\n<p>then the first two lines include the two different name spaces and you are able to call the same function names without having to worry that you are calling the wrong function.<\/p>\n<pre lang=\"php\">\r\necho codingfriends\\sayHello().\"<br\/>\"; \/\/ calling the codingfriends namespace\r\n<\/pre>\n<p>will call the sayHello() from within the namespace codingfriends<\/p>\n<pre lang=\"php\">\r\necho codingfriends\\sub\\sayHello().\"<br\/>\"; \/\/ call the codingfriends sub namespace\r\n<\/pre>\n<p>will call the namespace codingfriendssub sayHello function and the best thing is is if you are within a namespace and want to call a sub namespace within your namespace (it is like a tree with roots) you are able to do <\/p>\n<pre lang=\"php\">\r\n    echo sub\\sayHello();\r\n<\/pre>\n<p>from within the namespace codingfriends, it will find the codingfriends\\sub namespace and then call the sayHello() function within that namespace.<\/p>\n<p>The output would be for the index.php file<\/p>\n<pre lang=\"bash\">\r\nSay hello from codingfriends.\r\nSay hello from codingfriends sub module\r\nSay hello from codingfriends sub module<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP &#8211; namespace.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[390],"class_list":["post-1776","post","type-post","status-publish","format-standard","hentry","category-php","tag-namespace"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1776"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1776\/revisions"}],"predecessor-version":[{"id":1779,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1776\/revisions\/1779"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}