QUIZGUM

Coding Class

Quizgum : 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

  1. use namespace\class as short name;

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

  1. use \hello\hello as hh;

Let's see if it works by example.

  1. <?php
  2. namespace hello;
  3.  
  4. class hello
  5. {
  6. function hello()
  7. {
  8. return 'first hello class.';
  9. }
  10. }
  11.  
  12. namespace hello2;
  13.  
  14. class hello
  15. {
  16. function hello()
  17. {
  18. return 'second hello class.';
  19. }
  20. }
  21.  
  22. use \hello\hello as hh;
  23. use \hello2\hello as hh2;
  24.  
  25. $helloFirst = new hh;
  26. echo $helloFirst->hello();
  27. echo '<br>';
  28. $helloSecond = new hh2;
  29. echo $helloSecond->hello();
  30. ?>

Result

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