performance and the use statment PHP, Laravel 4.x - php

Are there performance considerations when importing classes with the use statement in PHP and more specifically laravel 4.x ?
for example often times i have many use statements in my controllers as such:
use OrganisationController;
use Input;
use Redirect;
use Validator;
use View;
use Organisation;
use Sentry;
use User;
use Str;
use Lang;
use Application;
use Job;
use Upload;
class ApplicationController extends OrganisationController {
...

In my opinion, it should not effect on performance because, when you use a class that is not in the same namespace you use it with a preceding backslash like
\View::make(...);
This way you just telling that, the View class is within the global namespace, to make the code clean you can use use statement top of your class like
use \View, \Redirect, \Validator; // more
class WhatEver extends BaseController {
function index()
{
//...
return View::make(...);
}
}
So, in either way, php is loading (include/require) the class whenever it founds any use of the class but when it's not in the scope of current namespace you must provide the namespace and it could be directly from the code like
\View::make(...);
Or maybe using a use statement to keep your code clean but in both ways, php is loading the class during run time (if not already loaded) but you have to provide the right location (with namespace).

Related

Do use in PHP "use" for internal php classes or backslash "\"

I wonder what the differences and consequences are in applying individual conventions.
I mean only the rule about internal classes of type DateTime, PDO, Exception etc.
<?php
namespace App;
class Foo
{
public function bar(\DateTime $baz): void
{
}
}
vs
<?php
namespace App;
use DateTime;
class Foo
{
public function bar(DateTime $baz): void
{
}
}
What are the arguments behind one or the other solution? Can you say that any solution is better?
Who use "\":
Symfony
Laravel
Who use "use":
?
If you are only using that imported class once, it's probably better to just call that class using \.
However, if you are using that class throughout your methods, it would make more sense to import it using use.
It's not framework-specific, it's case-dependant.
The reason for the \ is because when you're in a namespace, it tries importing classes from the same namespace. Using \ almost "resets" the namespace to top-level.
It does not make any difference even if you use internal classes or not.
When you import a class you should avoid leading backslash cause the path is supposed to be absolute
https://www.php.net/manual/en/language.namespaces.importing.php

How to shorthand imports of Models in PHP Lumen

I tried short handling my models in my controller since they are in the same folder. So Instead of doing these:
namespace App\Http\Controllers\Api;
use App\User;
use App\Pictures;
use App\Info;
I tried these:
namespace App\Http\Controllers\Api;
use App\{
User, Pictures, Info
};
Unfortunately it's not working. It shows that the controller doesn't read it when I short handed it. Any suggestions or reasons why this doesn't work?
In PHP V7.0, You can refer this method http://php.net/manual/en/language.namespaces.importing.php
<?php
// Pre PHP 7 code
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;
use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;
use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;
// PHP 7+ code
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};

"use" statements in php file

I am a beginner in Laravel and while learning about "namespace" and "use" statements, I found that, for example, in Controllers, when we first write "use" statements it should be repeated inside of the function.
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
Why is this working so? Why is not it enough to write "use" statements once without repeating in a function? And also, if I will create another Controller with the same namespace, should I write the same "use" statements there as well?
There are two different use in PHP:
To alias namespaced names,
to apply traits to classes.
The use at the top of the file aliases namespaced names into shorter local ones. Literally their only use is so you can write DispatchesJobs inside this one file instead of having to always use the fully qualified name \Illuminate\Foundation\Bus\DispatchesJobs.
use inside a class applies that trait to the class.
In this case you could omit the first use to alias the trait, and apply it using its fully qualified name:
namespace App\Http\Controllers;
class Controller extends \Illuminate\Routing\Controller {
use \Illuminate\Foundation\Bus\DispatchesJobs;
...
}
This does exactly the same thing, but is obviously rather verbose. Establishing a few aliases at the top of the file allows your following code to be terser.
Using keyword use outside class is just importing specific part called trait.
And using keyword use inside class is actually inheriting or implements to use that trait

Importing class in laravel 5.2

use App\User;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Response;
do i need to write the above import class every time when i create a new controller?In laravel 4.2 it does automatically
is any other way to do this?
Yes, you do. Your app is under the namespace App;, so if you want to use the Input class, but you don't say use Illuminate\Support\Facades\Input, PHP will try looking for it under your namespace App; and an exception will be thrown since the class you're looking for probably won't be there.
Those are not a mandatory class.
When ever you are using those facedes you have to add
There is one way i know.
class something like CustomController
Add the all the common classed in that controller.
Now for every controller you can extend the new CustomController

Laravel 5 - constantly repeating use statements

Is there a central place where I can put 'use' statements so I don't have to keep doing things like this with every single controller I create?
<?php namespace App/Http/Controllers
use Session;
use Auth;
use Input;
use Log;
use Carbon;
use Response;
use Illuminate\Routing\Controller;
class BlaBlaController extends Controller {}
Just seems to violate DRYness and seems inefficient.
Short answer: No.
The 'use' statements are resolving the namespaces for that file, so you can't inherit them from other files. It doesn't violate DRY because there isn't actually any logic that is being repeated.
Now if you don't want to have to include those use statement in every controller then you can just resolve the facade out of the global scope whenever you use it. For example the following will work from any namespace, without needing a use statement.
\Input::all();
In my opinion it looks a little cleaner to just include the use statement, but either will work.

Categories