Facebook Twitter Reset

This Tutorial Has Been Viewed 1,681 Times.

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.

VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

Incoming search terms:




You will also be interested in ,

No comments yet.

Leave a Comment

Spam protection by WP Captcha-Free

Affordable Seo PackagesSeo BlogEdu Backlinks
More in PHP (116 of 122 articles)