Tools and Services Blog Learn Quizzes Smile API Log In / Sign Up
Tools and Services Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated our privacy policy to let you know that we use cookies to personalise content and ads. We also use cookies to analyse our traffic and we share information about your use of our site and application with our advertising and analytics partners. By using this website or our application you agree to our use of cookies. Learn more about the way this website uses cookies or remove this message.

Overwrite built in PHP functions using namespaces

April 7, 2014 Difficulty: 50 / 50 Tweet

This web development tutorial will teach you about the role of namespaces in PHP through a very cool example - overwriting what is arguably the most frequently used function in PHP - the great var_dump() :).

Namespaces are a relatively new concept in PHP as they've been introduced in PHP 5.3. The role of namespaces is to allow virtually unlimited usage of code elements such as classes and functions. Namespaces in programming, not just PHP, are just like folders in operating systems - they have the role to organize your files.

To better understand the concept, let's imagine that you have a folder called songs and one called pictures. In each of them you keep a file called list.txt that contains the names off all your songs and all your images respectively. If the songs and images were both in the same folder you would be forced to rename on of your list.txt files because you can't have two files with the same name in the same folder.

In programming things are exactly the same: you can't have two classes with the same name because that would result in a FATAL Error so you are forced to either rename your classes or use namespaces.

Now let's move on to the code. Before php 5.3, If I wanted to write a function that "enhances" the functionality of var_dump() i would have done something like this:

  
    <?php
      function enhanced_var_dump($var)
      {
        echo "<pre>";
        var_dump($var);
        echo "</pre>";
        die;
      }

      $var = array(1,2,3,9,"any");
      enhanced_var_dump($var);
    ?>
  

By using namespaces, what I can do is simply enhance the var_dump() function itself. Here's how:

  
    <?php 
    namespace vardumps;

    function var_dump($var)
    {
      echo "<pre>";
        \var_dump($var);
      echo "</pre>";
      die;
    }

    $var = array(1,2,3,9,"any");
    var_dump($var);
    ?>
  

Here's what happens in the code above:

  • I first declare the namespace I'm in as vardumps
  • Then I create a function called var_dump within my namespace that echoes the pre tag and then calls the "original" var_dump() function from the GLOBAL namespace by prefixing it with a backslash (the namespace separator in PHP is just like the folder separators in Windows).

This web development tutorial is meant to show you the power that namespacing gives to the programmer so if you've enjoyed reading it go ahead and give me a big like on Facebook.

comments powered by Disqus