How to use Namespaces in php 5.3 ?
How would you differentiate if the call save() was to save a blogs or save comments? The solution was to use blog_save() or comment_save() before the introduction of classes in which we could write the save() function within the Blog class or the Comment class.Using classes is obviously a much more elegant solution.
Using namespaces, we could simply separate the two functions with the same name very easily:
< ?php
namespace Blog;
function save()
{
echo "Saving the blog!";
}
namespace Comment;
function save()
{
echo "Saving the comment!";
}
// To invoke the functions
Blog\save(); // This prints - Saving the blog!
Comment\save(); // This prints - Saving the comment!
?>
Developers will have to use \ backslash operator to dereference namespaces.
Related posts:
