Difference between namespace and use especially in php laravel framework









up vote
0
down vote

favorite












What's the difference between namespace and use, I can't understand it, I thought use with 'AS' is for making a shortcut for a namespace



i thought for using use without for namespace you have to include/require something, but this example in the screenshot just confuses me



Screenshot from 7:11 https://www.youtube.com/watch?v=sLFNVXY0APk










share|improve this question























  • A namespace is like a container, a use statement is like including another container inside the one you are in. It's a method to prevent naming conflicts if for example 2 classes have the same name, if the namespace is different everything is cool. Importing( include/require) is a whole other topic.
    – ArtisticPhoenix
    Nov 10 at 21:27











  • namespace is like "This class lives here." use is like "Get me a class that lives somewhere else."
    – newUserName02
    Nov 10 at 21:29










  • With AS it is a shortcut to a namespace, without it if It includes the class name, then its a shortcut to the class....
    – ArtisticPhoenix
    Nov 10 at 21:31














up vote
0
down vote

favorite












What's the difference between namespace and use, I can't understand it, I thought use with 'AS' is for making a shortcut for a namespace



i thought for using use without for namespace you have to include/require something, but this example in the screenshot just confuses me



Screenshot from 7:11 https://www.youtube.com/watch?v=sLFNVXY0APk










share|improve this question























  • A namespace is like a container, a use statement is like including another container inside the one you are in. It's a method to prevent naming conflicts if for example 2 classes have the same name, if the namespace is different everything is cool. Importing( include/require) is a whole other topic.
    – ArtisticPhoenix
    Nov 10 at 21:27











  • namespace is like "This class lives here." use is like "Get me a class that lives somewhere else."
    – newUserName02
    Nov 10 at 21:29










  • With AS it is a shortcut to a namespace, without it if It includes the class name, then its a shortcut to the class....
    – ArtisticPhoenix
    Nov 10 at 21:31












up vote
0
down vote

favorite









up vote
0
down vote

favorite











What's the difference between namespace and use, I can't understand it, I thought use with 'AS' is for making a shortcut for a namespace



i thought for using use without for namespace you have to include/require something, but this example in the screenshot just confuses me



Screenshot from 7:11 https://www.youtube.com/watch?v=sLFNVXY0APk










share|improve this question















What's the difference between namespace and use, I can't understand it, I thought use with 'AS' is for making a shortcut for a namespace



i thought for using use without for namespace you have to include/require something, but this example in the screenshot just confuses me



Screenshot from 7:11 https://www.youtube.com/watch?v=sLFNVXY0APk







php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 21:27

























asked Nov 10 at 21:25









Juri Jurka

1113




1113











  • A namespace is like a container, a use statement is like including another container inside the one you are in. It's a method to prevent naming conflicts if for example 2 classes have the same name, if the namespace is different everything is cool. Importing( include/require) is a whole other topic.
    – ArtisticPhoenix
    Nov 10 at 21:27











  • namespace is like "This class lives here." use is like "Get me a class that lives somewhere else."
    – newUserName02
    Nov 10 at 21:29










  • With AS it is a shortcut to a namespace, without it if It includes the class name, then its a shortcut to the class....
    – ArtisticPhoenix
    Nov 10 at 21:31
















  • A namespace is like a container, a use statement is like including another container inside the one you are in. It's a method to prevent naming conflicts if for example 2 classes have the same name, if the namespace is different everything is cool. Importing( include/require) is a whole other topic.
    – ArtisticPhoenix
    Nov 10 at 21:27











  • namespace is like "This class lives here." use is like "Get me a class that lives somewhere else."
    – newUserName02
    Nov 10 at 21:29










  • With AS it is a shortcut to a namespace, without it if It includes the class name, then its a shortcut to the class....
    – ArtisticPhoenix
    Nov 10 at 21:31















A namespace is like a container, a use statement is like including another container inside the one you are in. It's a method to prevent naming conflicts if for example 2 classes have the same name, if the namespace is different everything is cool. Importing( include/require) is a whole other topic.
– ArtisticPhoenix
Nov 10 at 21:27





A namespace is like a container, a use statement is like including another container inside the one you are in. It's a method to prevent naming conflicts if for example 2 classes have the same name, if the namespace is different everything is cool. Importing( include/require) is a whole other topic.
– ArtisticPhoenix
Nov 10 at 21:27













namespace is like "This class lives here." use is like "Get me a class that lives somewhere else."
– newUserName02
Nov 10 at 21:29




namespace is like "This class lives here." use is like "Get me a class that lives somewhere else."
– newUserName02
Nov 10 at 21:29












With AS it is a shortcut to a namespace, without it if It includes the class name, then its a shortcut to the class....
– ArtisticPhoenix
Nov 10 at 21:31




With AS it is a shortcut to a namespace, without it if It includes the class name, then its a shortcut to the class....
– ArtisticPhoenix
Nov 10 at 21:31












2 Answers
2






active

oldest

votes

















up vote
0
down vote













In PHP a namespace is a container that encapsulates some "stuff": functions classes, interfaces or traits. It's done to avoid naming conflicts, because in PHP (or most languages) you can only declare a class/function/interface/trait one time and one time only.



So for example, you could have a class named Users and another class called MyProjectUsers because the second has a namespace this prevents conflict with the first. Otherwise (without the namespace) it would be ambiguous which one you wanted and so PHP will give an error if you try it.



A Use statement allows you to Use a namespace without referring to the whole thing. If you are in the global or base namespace these 2 things are roughly equivalent



 new MyProjectUsers();


AND



use MyProjectUsers
new Users();


The big advantage to the second one is if you decide to change the namespace, you only have to change it in the Use statement and not every class call within the namespace. Beyond that it's much less to type, and we programmers don't like to type if we don't have to.



Now as for the AS keyword, you can use this to resolve conflits, or to just shorten namespaces for example:



 use MyProject as M; //now M = MyProject 

new MUsers(); // MyProject user class
new Users(); //base namespace user class


This really has nothing to do with how you import the code into PHP, however there are some naming conventions (like PSR4) that deal with properly placing files according to their namespace. But that doesn't change depending how you make use of Use. The general "rule of thumb" is that the namespace should match the directory structure, that way we can make use of auto loaders as opposed to including every file at the beginning. Autoloaders only load the class file when it's needed, and as I said this is based of properly naming and namespacing the class and directory structure.



I hope that makes sense.






share|improve this answer





























    up vote
    0
    down vote













    These keywords aren’t for Laravel. They are related to the PHP itself.



    namespaces are like the place of the current class. but The “use” keyword tells PHP that you are using a specific file or dependency (can be a class, trait, interface, etc). so the “namespace” is used to encapsulate an item (i.e., class, trait, interface…).




    in PHP, namespaces are designed to solve two problems that authors of
    libraries and applications encounter when creating re-usable code
    elements such as classes or functions




    for example:
    When you define a class, it can only have access to other classes within its namespaces. Your controller for instance is defined within the following namespace.



    namespace AppHttpControllersAuth;


    So to use other classes, you need to import them from their own namespaces so you can access them. e.g.:



    use IlluminateFoundationAuthThrottlesLogins;


    For another example if we have a Person class and it is in app folder, it's namespace will be AppPerson. And when you want to use that class you should use it by the keyword use. And that will be use AppPerson. It will load that class to your current file.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243555%2fdifference-between-namespace-and-use-especially-in-php-laravel-framework%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      In PHP a namespace is a container that encapsulates some "stuff": functions classes, interfaces or traits. It's done to avoid naming conflicts, because in PHP (or most languages) you can only declare a class/function/interface/trait one time and one time only.



      So for example, you could have a class named Users and another class called MyProjectUsers because the second has a namespace this prevents conflict with the first. Otherwise (without the namespace) it would be ambiguous which one you wanted and so PHP will give an error if you try it.



      A Use statement allows you to Use a namespace without referring to the whole thing. If you are in the global or base namespace these 2 things are roughly equivalent



       new MyProjectUsers();


      AND



      use MyProjectUsers
      new Users();


      The big advantage to the second one is if you decide to change the namespace, you only have to change it in the Use statement and not every class call within the namespace. Beyond that it's much less to type, and we programmers don't like to type if we don't have to.



      Now as for the AS keyword, you can use this to resolve conflits, or to just shorten namespaces for example:



       use MyProject as M; //now M = MyProject 

      new MUsers(); // MyProject user class
      new Users(); //base namespace user class


      This really has nothing to do with how you import the code into PHP, however there are some naming conventions (like PSR4) that deal with properly placing files according to their namespace. But that doesn't change depending how you make use of Use. The general "rule of thumb" is that the namespace should match the directory structure, that way we can make use of auto loaders as opposed to including every file at the beginning. Autoloaders only load the class file when it's needed, and as I said this is based of properly naming and namespacing the class and directory structure.



      I hope that makes sense.






      share|improve this answer


























        up vote
        0
        down vote













        In PHP a namespace is a container that encapsulates some "stuff": functions classes, interfaces or traits. It's done to avoid naming conflicts, because in PHP (or most languages) you can only declare a class/function/interface/trait one time and one time only.



        So for example, you could have a class named Users and another class called MyProjectUsers because the second has a namespace this prevents conflict with the first. Otherwise (without the namespace) it would be ambiguous which one you wanted and so PHP will give an error if you try it.



        A Use statement allows you to Use a namespace without referring to the whole thing. If you are in the global or base namespace these 2 things are roughly equivalent



         new MyProjectUsers();


        AND



        use MyProjectUsers
        new Users();


        The big advantage to the second one is if you decide to change the namespace, you only have to change it in the Use statement and not every class call within the namespace. Beyond that it's much less to type, and we programmers don't like to type if we don't have to.



        Now as for the AS keyword, you can use this to resolve conflits, or to just shorten namespaces for example:



         use MyProject as M; //now M = MyProject 

        new MUsers(); // MyProject user class
        new Users(); //base namespace user class


        This really has nothing to do with how you import the code into PHP, however there are some naming conventions (like PSR4) that deal with properly placing files according to their namespace. But that doesn't change depending how you make use of Use. The general "rule of thumb" is that the namespace should match the directory structure, that way we can make use of auto loaders as opposed to including every file at the beginning. Autoloaders only load the class file when it's needed, and as I said this is based of properly naming and namespacing the class and directory structure.



        I hope that makes sense.






        share|improve this answer
























          up vote
          0
          down vote










          up vote
          0
          down vote









          In PHP a namespace is a container that encapsulates some "stuff": functions classes, interfaces or traits. It's done to avoid naming conflicts, because in PHP (or most languages) you can only declare a class/function/interface/trait one time and one time only.



          So for example, you could have a class named Users and another class called MyProjectUsers because the second has a namespace this prevents conflict with the first. Otherwise (without the namespace) it would be ambiguous which one you wanted and so PHP will give an error if you try it.



          A Use statement allows you to Use a namespace without referring to the whole thing. If you are in the global or base namespace these 2 things are roughly equivalent



           new MyProjectUsers();


          AND



          use MyProjectUsers
          new Users();


          The big advantage to the second one is if you decide to change the namespace, you only have to change it in the Use statement and not every class call within the namespace. Beyond that it's much less to type, and we programmers don't like to type if we don't have to.



          Now as for the AS keyword, you can use this to resolve conflits, or to just shorten namespaces for example:



           use MyProject as M; //now M = MyProject 

          new MUsers(); // MyProject user class
          new Users(); //base namespace user class


          This really has nothing to do with how you import the code into PHP, however there are some naming conventions (like PSR4) that deal with properly placing files according to their namespace. But that doesn't change depending how you make use of Use. The general "rule of thumb" is that the namespace should match the directory structure, that way we can make use of auto loaders as opposed to including every file at the beginning. Autoloaders only load the class file when it's needed, and as I said this is based of properly naming and namespacing the class and directory structure.



          I hope that makes sense.






          share|improve this answer














          In PHP a namespace is a container that encapsulates some "stuff": functions classes, interfaces or traits. It's done to avoid naming conflicts, because in PHP (or most languages) you can only declare a class/function/interface/trait one time and one time only.



          So for example, you could have a class named Users and another class called MyProjectUsers because the second has a namespace this prevents conflict with the first. Otherwise (without the namespace) it would be ambiguous which one you wanted and so PHP will give an error if you try it.



          A Use statement allows you to Use a namespace without referring to the whole thing. If you are in the global or base namespace these 2 things are roughly equivalent



           new MyProjectUsers();


          AND



          use MyProjectUsers
          new Users();


          The big advantage to the second one is if you decide to change the namespace, you only have to change it in the Use statement and not every class call within the namespace. Beyond that it's much less to type, and we programmers don't like to type if we don't have to.



          Now as for the AS keyword, you can use this to resolve conflits, or to just shorten namespaces for example:



           use MyProject as M; //now M = MyProject 

          new MUsers(); // MyProject user class
          new Users(); //base namespace user class


          This really has nothing to do with how you import the code into PHP, however there are some naming conventions (like PSR4) that deal with properly placing files according to their namespace. But that doesn't change depending how you make use of Use. The general "rule of thumb" is that the namespace should match the directory structure, that way we can make use of auto loaders as opposed to including every file at the beginning. Autoloaders only load the class file when it's needed, and as I said this is based of properly naming and namespacing the class and directory structure.



          I hope that makes sense.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 21:48

























          answered Nov 10 at 21:41









          ArtisticPhoenix

          14.7k11223




          14.7k11223






















              up vote
              0
              down vote













              These keywords aren’t for Laravel. They are related to the PHP itself.



              namespaces are like the place of the current class. but The “use” keyword tells PHP that you are using a specific file or dependency (can be a class, trait, interface, etc). so the “namespace” is used to encapsulate an item (i.e., class, trait, interface…).




              in PHP, namespaces are designed to solve two problems that authors of
              libraries and applications encounter when creating re-usable code
              elements such as classes or functions




              for example:
              When you define a class, it can only have access to other classes within its namespaces. Your controller for instance is defined within the following namespace.



              namespace AppHttpControllersAuth;


              So to use other classes, you need to import them from their own namespaces so you can access them. e.g.:



              use IlluminateFoundationAuthThrottlesLogins;


              For another example if we have a Person class and it is in app folder, it's namespace will be AppPerson. And when you want to use that class you should use it by the keyword use. And that will be use AppPerson. It will load that class to your current file.






              share|improve this answer


























                up vote
                0
                down vote













                These keywords aren’t for Laravel. They are related to the PHP itself.



                namespaces are like the place of the current class. but The “use” keyword tells PHP that you are using a specific file or dependency (can be a class, trait, interface, etc). so the “namespace” is used to encapsulate an item (i.e., class, trait, interface…).




                in PHP, namespaces are designed to solve two problems that authors of
                libraries and applications encounter when creating re-usable code
                elements such as classes or functions




                for example:
                When you define a class, it can only have access to other classes within its namespaces. Your controller for instance is defined within the following namespace.



                namespace AppHttpControllersAuth;


                So to use other classes, you need to import them from their own namespaces so you can access them. e.g.:



                use IlluminateFoundationAuthThrottlesLogins;


                For another example if we have a Person class and it is in app folder, it's namespace will be AppPerson. And when you want to use that class you should use it by the keyword use. And that will be use AppPerson. It will load that class to your current file.






                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  These keywords aren’t for Laravel. They are related to the PHP itself.



                  namespaces are like the place of the current class. but The “use” keyword tells PHP that you are using a specific file or dependency (can be a class, trait, interface, etc). so the “namespace” is used to encapsulate an item (i.e., class, trait, interface…).




                  in PHP, namespaces are designed to solve two problems that authors of
                  libraries and applications encounter when creating re-usable code
                  elements such as classes or functions




                  for example:
                  When you define a class, it can only have access to other classes within its namespaces. Your controller for instance is defined within the following namespace.



                  namespace AppHttpControllersAuth;


                  So to use other classes, you need to import them from their own namespaces so you can access them. e.g.:



                  use IlluminateFoundationAuthThrottlesLogins;


                  For another example if we have a Person class and it is in app folder, it's namespace will be AppPerson. And when you want to use that class you should use it by the keyword use. And that will be use AppPerson. It will load that class to your current file.






                  share|improve this answer














                  These keywords aren’t for Laravel. They are related to the PHP itself.



                  namespaces are like the place of the current class. but The “use” keyword tells PHP that you are using a specific file or dependency (can be a class, trait, interface, etc). so the “namespace” is used to encapsulate an item (i.e., class, trait, interface…).




                  in PHP, namespaces are designed to solve two problems that authors of
                  libraries and applications encounter when creating re-usable code
                  elements such as classes or functions




                  for example:
                  When you define a class, it can only have access to other classes within its namespaces. Your controller for instance is defined within the following namespace.



                  namespace AppHttpControllersAuth;


                  So to use other classes, you need to import them from their own namespaces so you can access them. e.g.:



                  use IlluminateFoundationAuthThrottlesLogins;


                  For another example if we have a Person class and it is in app folder, it's namespace will be AppPerson. And when you want to use that class you should use it by the keyword use. And that will be use AppPerson. It will load that class to your current file.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 21:49

























                  answered Nov 10 at 21:32









                  ali sharifi

                  30419




                  30419



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243555%2fdifference-between-namespace-and-use-especially-in-php-laravel-framework%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      政党