You are on page 1of 12

(questionaire = q)

General PHP

1 - what are traits in php?

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A
Trait is intended to reduce some limitations of single inheritance by enabling a
developer to reuse sets of methods freely in several independent classes living in
different class hierarchies. The semantics of the combination of Traits and classes is
defined in a way which reduces complexity, and avoids the typical problems associated
with multiple inheritance and Mixins.

A Trait is similar to a class, but only intended to group functionality in a fine-grained and
consistent way. It is not possible to instantiate a Trait on its own. It is an addition to
traditional inheritance and enables horizontal composition of behavior; that is, the
application of class members without requiring inheritance.

2 - What is Dependency Injection?


Dependency injection. ... A dependency is an object that can be
used (a service). An injection is the passing of a dependency to a
dependent object (a client) that would use it.

The Laravel service container is a powerful tool for managing class


dependencies and performing dependency injection. Dependency injection is
a fancy phrase that essentially means this: class dependencies are "injected"
into the class via the constructor or, in some cases, "setter" methods.
Constructor Injection / Method Injection

3 - What is abstract Class, Interface class and


implementation?
abstract class: you can not create an object of that class.
interface: By implementation of interface in php class you are specifying set of the
method which classes must implement.

interface abc
{
public function xyz($b);
}

class test implements abc


{
public function xyz($b)
{
//your function body
}
}

4 - Can there be any abstract methods in abstract class?


Yes.

5- Can there be any abstract methods in interface class?


Yes. all methods are abstract.

Note: In abstract classes, this is not necessary that every method should be
abstract. But in interface every method is abstract.
6 - Difference between unset()/unlink()
7 - Final Class/Method and what error can it produce?
8 - What are PSRs?
PSRs are PHP Standards Recommendations that aim at standardising common
aspects of PHP Development.

9 - Have you followed any PSR for your projects if so can you
state some of the points.

Laravel

Base:
1- How long have you been using Laravel?
2- Why Laravel over other PHP frameworks?
3- What is your favorite feature of Laravel?
4- Have you used Lumen before?
Lumen is the micro-framework by Laravel that was made by
Taylor specifically for APIs and microservices. If they've decided to use
Lumen over Larvel for a microservice or API, it shows that they care
about performance.

Available Router Methods(q)


Route::get($uri, $callback);

Route::post($uri, $callback);

Route::put($uri, $callback);

Route::patch($uri, $callback);

Route::delete($uri, $callback);

Route::options($uri, $callback);

Route::match(['get', 'post'], '/', function () {

//

});

Route::any('foo', function () {

//

});

5 - Explain Service Containers/Service Providers


a - service providers:
Service providers are the central place of all Laravel application
bootstrapping. Your own application, as well as all of Laravel's core services
are bootstrapped via service providers.
But, what do we mean by "bootstrapped"? In general, we
mean registering things, including registering service container bindings,
event listeners, middleware, and even routes. Service providers are the
central place to configure your application.

If you open the config/app.php file included with Laravel, you will see
a providers array. These are all of the service provider classes that will be
loaded for your application. Of course, many of these are "deferred" providers,
meaning they will not be loaded on every request, but only when the services
they provide are actually needed.

b - service containers:
The Laravel service container is a powerful tool for managing class
dependencies and performing dependency injection. Dependency injection is
a fancy phrase that essentially means this: class dependencies are "injected"
into the class via the constructor or, in some cases, "setter" methods.

6-What are Facades?


Facades provide a "static" interface to classes that are available in the
application's service container. Laravel ships with many facades which
provide access to almost all of Laravel's features. Laravel facades serve as
"static proxies" to underlying classes in the service container, providing the
benefit of a terse, expressive syntax while maintaining more testability and
flexibility than traditional static methods.

All of Laravel's facades are defined in


the Illuminate\Support\Facades namespace. So, we can easily access a facade
like so:

use Illuminate\Support\Facades\Cache;
Route::get('/cache', function () {

return Cache::get('key');

});

7-What are Contracts?


Laravel's Contracts are a set of interfaces that define the core services
provided by the framework. For example,
a Illuminate\Contracts\Queue\Queue contract defines the methods needed for
queueing jobs, while the Illuminate\Contracts\Mail\Mailer contract defines the
methods needed for sending e-mail.

8. Diff b/w Contracts Vs. Facades


Laravel's facades and helper functions provide a simple way of utilizing
Laravel's services without needing to type-hint and resolve contracts out of the
service container. In most cases, each facade has an equivalent contract.

Unlike facades, which do not require you to require them in your class'
constructor, contracts allow you to define explicit dependencies for your
classes. Some developers prefer to explicitly define their dependencies in this
way and therefore prefer to use contracts, while other developers enjoy the
convenience of facades.
9. what is Method Spoofing?
HTML forms do not support PUT, PATCH or DELETE actions. So, when
defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a
hidden _method field to the form.

10. Laravel 5 has built in CSRF protection on every route. How


would I go about turning that off?
Hopefully they mention that CSRF protection is important, but
since we're asking them to turn it off they might not mention it which
is OK. You'd easily turn it off by going into Kernel.php and removing it
from the middleware stack that is ran on every route.
This is also a good place to ask about middleware in general and make
sure they both understand what middleware is and why it's important.

11. Have you written any custom middleware for a Laravel


applicaton? If so, tell me about a time you did.

If they've never had to write any custom middleware then they most
likely have not written any complex applications, or were not the lead
engineer on a complex application project.

12. What are Collections?

The Illuminate\Support\Collection class provides a fluent, convenient wrapper


for working with arrays of data. For example, check out the following code.
We'll use the collect helper to create a new collection instance from the array,
run the strtoupper function on each element, and then remove all empty
elements:

$collection = collect(['taylor', 'abigail', null])->map(function ($name) {

return strtoupper($name);

})

->reject(function ($name) {

return empty($name);

});

As you can see, the Collection class allows you to chain its methods to
perform fluent mapping and reducing of the underlying array. In general,
collections are immutable, meaning every Collectionmethod returns an entirely
new Collection instance.

As mentioned above, the collect helper returns a


new Illuminate\Support\Collection instance for the given array. So, creating a
collection is as simple as:

$collection = collect([1, 2, 3]);

13. Laravel Collection Tap/Pipe Methods.

Tap: Laravel 5.4.10 introduces a new tap method on collections which allow you to tap
into the collection at a specific point and do something with the results while not
affecting the main collection.
Pipe: Laravel also provides another similar method to tap named pipe and they are
similar in that they can be performed inside a collection pipeline. However, they have
two primary differences:

Tap allows you to do something with the data available but it does not modify the
original. Pipe modifies the results in the collection based on its return value.

14. what are Laravel Mailables


A new feature in Laravel 5.3 is a way to simplify sending email by creating mailable
classes that handle setting up your emails.

The best way to explain this feature is with an example. In Laravel 5.2 you would
typically send an email like the following that is outlined in the documentation:

Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {

$m->from('hello@app.com', 'Your Application');

$m->to($user->email, $user->name)->subject('Your Reminder!');

});

php artisan make:mail YourReminder

15. Form Request Validation

16. How do I customize validation error messages for a form?


You may customize the error messages used by the form request by
overriding the messagesmethod. This method should return an array of attribute
/ rule pairs and their corresponding error messages:

public function messages()

return [

'title.required' => 'A title is required',

'body.required' => 'A message is required',

];

17. How do I see all of the routes that are defined?

$ php artisan routes

18. How would you add a 3rd party package like Sentry?

using composer registering service providers in Laravel)**


Laravel Queries/Eloquents

1 - Get Last row from the table users. (q)


DB::table('users')->orderBy('id', 'desc')->first();

OR

User::all() -> last();

2 - Get the second last record from database (q)

$user = User::where('simple', true)->orderBy('created_at',

'desc')->skip(1)->take(1)->get();

You might also like