Why do we use \ instead of the namespace? - laravel

I am new to Laravel and I have seen that when we use Query Builder instead of Eloquent as is required we must add the namespace i.e. use Illuminate\Support\Facades\DB; or use DB;(As it is mentioned in Aliases) inorder to use it, some people simply use \DB while writing a query.
Eg. \DB->table('tasks')->where('id', 3)->get();
What does \ denote in namespacing convention or what does it mean here. While finding answers someone told me, \ means Global Namespace, but if that's true what does it actually mean in this context? I used \ in place of App\Model_Name and I got an error. Why s that so if \ means Global Namespace?

PHP has a trick for referring to classes that are located within the global namespace, we simply prefix them with a backward (\) slash.
With the leading backward (\) slash, PHP knows that we are referring to the Class in the global namespace, and instantiates that one.
You will have an error while using (\) when some classes have same Class names.
Example:
Request class has many namespaces Illuminate\Support\Facades\Request and Illuminate\Http\Request and when you are trying to use Request class for
Illuminate\Http\Request class and you have defined \Request instead of using a namespace, the conflict will happen and you will get an error with no class found.
This is my belief. Hope this stuff will help you little to understand.

Related

does not comply with psr-4 autoloading standard. Skipping issue in Laravel Composer after updating composer [duplicate]

When running composer's update, install, require, dump-autoload, etc.; I suddenly start getting a yellow deprecation notice that says:
Class Foo\Bar\Baz located in ./foo/bar/utility/baz.php does not comply with psr-4 autoloading standard. Skipping.
Before Composer 2.0, one used to get:
Deprecation Notice: Class Foo\Bar\Baz located in ./foo/bar/Baz.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///usr/local/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Why am I getting this notice or warning? What do I need to get rid of it and get ready for Composer 2.0?
This can happen for a variety of reasons.
The important thing is to pay attention to the error message which generally points very accurately to the source of the issue.
Path Case
The most common reason for this is that, as shown in the error message, the case for the different components of the pathname for Bar.php do not match with the case for the fully qualified class name;
foo/bar/Baz.php does not match App\Bar\Baz.
Simply update your application or package so that each path component matches the case of its the namespace it holds:
Foo\Bar\Baz.php
File name and Class name or Namespace differences
Check the pathname against the namespace very carefully. Sometimes your named your class (or your namespace) FooBar, but its path on disk is "foo-bar", for example. Or simply for any reason your namespace does not fully match the pathname of the files.
This will trigger a notice/warning as well. You need to either rename the files or rename the classes (or namespaces).
Usually, changing the path or files is much easier, since changing the class or namespace names would require you refactor code to match the new names, whereas changing the paths will not need you to refactor anything.
Nested namespaces and missing declaration
Let's say that you have:
"autoload": {
"psr-4": {
"Fizz\\Buzz\\": "src/"
}
},
And the class Dummy, defined inside src/Buzz:
// src/Buzz/Dummy.php
namespace Fizz\Buzz
class Dummy {}
The above will work, but will throw the notice like the others. The correct way would be:
// src/Buzz/Dummy.php
namespace Fizz\Buzz\Buzz
class Dummy {}
You'll need not only to make the change on the affected class, but on any other file where this class is used or imported. (e.g. by now declaring use Fizz\Buzz\Buzz\Dummy;).

In laravel difference as describe below

(1). when i use \Auth::user()->email
need not to specify
use Auth;
at top of the file
(2). when i use only Auth::user()->email
need to specify
use Auth;
at top of the file.
which one is best for performance
(1). \Auth or
(2) Auth
Thanks in advance
There is no difference, or at least any impact that you could observe.
use Auth;
Is better for file readers, sometimes when you need to refractor your code a look on use list make it faster to see what models are related to your controller.
Moreover you can set an alias to your included classes, like
use Illuminate\Http\Request as IlluminateRequest;
use Request as FacadeRequest;
Which comes in handy when dealing with specific cases.
Anyway, the short answer is that there is no difference that you can notice in terms of performance between both ways, and when you think twice then you can find more arguments for using a
use Auth;
solution.
My general rule is if I'm only going to use the class once in a file, then I'll usually just prepend the \ (like \Auth::user().
However, if I use it twice or more, then I'll import it (use Auth);
That said, either is fine.

Using phpseclib in model laravel 5.0

I'm trying to include Crypt/RSA.php in my model, but this always appear "AppClass 'App\Crypt_RSA' not found". Any idea? Thanks in advance
That error appears because you're trying to instantiate a the Crypt_RSA class in the App namespace where it doesn't exist, because the Crypt_RSA class is defined in the global namespace. So you can do one of two things:
1. Write a use statement at the top of your file:
use Crypt_RSA;
2. Prepend a backslash to the class name when using it:
new \Crypt_RSA();
You can read more on how namespaces work in the PHP Namespaces Documentation.

Laravel constants in class Facades

I have a class called Awesome and have used the ServiceProvider and the Facade to register it to the app. Now I can use it as Awesome::Things().
I want to add constants to this class, so I tried
<?php namespace Helper\Awesome;
class Awesome()
{
public static $MOVIE = 'I love the Lego Movie!";
}
but when I call Awesome::$MOVIE, I get Access to undeclared static property: Helper\\Aesome\\Facades\\AwesomeFacade::$MOVIE
Can someone help?
The short version is -- you don't really want to do that. Laravel facades aren't mean to be used like normal classes, and if your application uses them that way you'll likely confuse future developers.
Warning out of the way. When you create a "facade" in Laravel, you're actually creating a class alias. When you added Awesome to the alias list in app/config/app.php, at some point code like the following ran
class_alias('Helper\Aesome\Facades\AwesomeFacade','Awesome');
That means whenever you use a global non-namespaced class Awesome, PHP substitutes Helper\Aesome\Facades\AwesomeFacade. If you wanted to add constants, you'd need to add them to this class.
Laravel's able to pass through methods because of the base Facade class implements a __callStatic method that passes on your call to the actual service implementation object. Facades don't pass on static constant access. Additionally, PHP does not (appear to?) have similar magic methods for passing along requests for constants.
If you're curious about the in depth version of this answer, I'm currently writing a series on Laravel's object system, including some in-depth information about the facade implementation.

Laravel: conflict between model name and built-in facade

I have a Model in my Laravel app called Event. As I just discovered, this creates a conflict between my model and Illuminate\Support\Facades\Event, a built-in facade. The obvious solution here is to either change the name of my Model, which is not ideal because there is really no other name I could give my Model that makes any sense, or to rename the alias in app.php for Illuminate\Support\Facades\Event, which I'd like to avoid for fear of breaking anything that may rely on that alias in the future (I'm afraid I may forget).
It's been suggested that perhaps I could use namespaces, which I attempted as follows:
app/models/Event.php
namespace Models; #<-- I've also tried using "\Models" here
class Event extends \Eloquent{
app/database/seeds/DatabaseSeeder.php
Models\Event::create(); #<-- again, I've also used "\Models\Event"
All 4 combinations above have yielded a Class 'Models\Event' not found error when I run php artisan db:seed.
Perhaps I simply don't understand namespaces properly, but the more pressing issue is how to solve my problem. If it can be solved using namespaces as suggested, great, but I'm open to any other ideas as well.
I made this mistake early on as well, not necessarily understanding the role of namespace throughout the entire app.
The namespace should mark the business logic within the domain or responsibility of the app itself, so giving a namespace of Models isn't necessarily useful. Instead create a root namespace named after the app, your company, you, or whatever you like, then provide a Model sub-namespace.
For example:
namespace MyGreatApp\Models;
class Event extends \Eloquent{ }
Then you would reference this model under:
use MyGreatApp\Models\Event;
$event = new Event();
In the long run this is a cleaner and more organized approach. This does mean moving your models into a different folder, though. But there's nothing wrong with that. At least that way you know you have all your custom code in your MyGreatApp namespace. :)

Resources