CLASS web

PHP

Using namespace shortly

using namespace shortly

Earlier, we learned how to use namespaces.
Let's learn how to use namespaces shortly. Use the use keyword.

How to use keyword

use namespace\class as short name;  

For example, if you shorten \hello\hello, the namespace you used earlier to hh,

use \hello\hello as hh;

Let's see if it works by example.

<?php
    namespace hello;

    class hello
    {
        function hello()
        {
            return 'first hello class.';
        }
    }

    namespace hello2;

    class hello
    {
        function hello()
        {
            return 'second hello class.';
        }
    }

    use \hello\hello as hh;
    use \hello2\hello as hh2;

    $helloFirst = new hh;
    echo $helloFirst->hello();
    echo '<br>';
    $helloSecond = new hh2;
    echo $helloSecond->hello();
?>

I'll check the results right below.







Result

If you use this once, you can save a little bit later.





ALL COMMENTS 0

Sort by

PinkCoding

PinkCoding

X

PinkCoding

Web Scratch Pad

X

loading

            
            
        

Back to the course