PHP – Use

From a follow on from my previous post about namespace, when you want to use another name, shorter than the actual namespace of the namespace you are able to use “use”. Probably best to show in code

include "codingfriends.php";
use codingfriends as cf;
echo cf\sayHello()."<br/>";

the codingfriends.php file is

namespace codingfriends;
function sayHello()
    echo "Say hello from codingfriends.";    
function sayHelloFromSub()
    echo sub\sayHello();

so that the “use” will alter the namespace of codingfriends to cf instead so that you are able to call codingfriends as cf, so when

echo cf\sayHello()."<br/>";

is called it will call the codingfriends\sayHello() function.

Leave a Reply

Your email address will not be published. Required fields are marked *