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
php laravel
add a comment |
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
php laravel
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
add a comment |
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
php laravel
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
php laravel
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 10 at 21:48
answered Nov 10 at 21:41
ArtisticPhoenix
14.7k11223
14.7k11223
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 10 at 21:49
answered Nov 10 at 21:32
ali sharifi
30419
30419
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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