All posts

Senior Backend Interview: Trick Questions — HTTP Methods, the Request and Visibility

POST vs PUT vs PATCH, GET vs POST, HEAD and OPTIONS, the Laravel Request, readonly, property hooks and invokable controllers.

Marijan Kopčić••3 min read
Senior Backend Interview: Trick Questions — HTTP Methods, the Request and Visibility

The second part of preparation is for questions that look simple but have a catch. This is where most time gets lost, because you start explaining from the very beginning.

POST vs PUT vs PATCH

  • POST — creates a new resource on a collection: POST /reservations → 201 Created + a Location header. Sent twice, it creates two records. It is also used for actions that aren't CRUD: POST /reservations/12/cancellation.
  • PUT — replaces the whole resource at a known URI: PUT /reservations/12 with the complete object. A field you don't send is lost or reset to its default. Idempotent.
  • PATCH — changes only the fields sent: PATCH /reservations/12 with {"status": "confirmed"}. By definition it isn't necessarily idempotent (e.g. "increment by 1"), but in practice it usually is.

POST creates, PUT replaces the whole resource, PATCH changes part of it. PUT is idempotent, POST isn't. In practice I use PATCH for edits because the client sends only what changed.

Can PUT create a resource?

It can — if the client knows the URI, e.g. PUT /users/42/settings. Then it is an upsert: if it exists, replace it; if not, create it.

In Laravel

Route::apiResource() binds the update method to both PUT and PATCH. HTML forms only know GET and POST, so PUT, PATCH and DELETE are sent as POST with a hidden _method field — @method('PUT') in Blade. This is called method spoofing.

GET vs POST

GET has no body, parameters go in the URL, it can be cached and it stays in the browser history and in server logs — so never put passwords or tokens in a GET. POST carries data in the body and isn't cached.

HEAD and OPTIONS

HEAD is a GET without a body — headers only. OPTIONS returns the allowed methods; the browser sends it as a CORS preflight before non-simple cross-origin requests.

The Request — where each value is read from (Laravel)

  • $request->input('x') reads both the query string and the body.
  • $request->query('x') reads only the query string.
  • $request->route('id') reads a route parameter.
  • $request->validated() returns only the fields that passed validation — that is what I pass to the model, never $request->all().

Visibility — more trick questions

Can a subclass change a method's visibility?

It can only widen it — protected to public. It can't narrow it, because the subclass would then break the parent's contract. The same goes for interfaces: interface methods are always public.

Can a trait see a class's private members?

Yes — at compile time the trait is effectively copied into the class, so its methods have access to that class's private properties too.

readonly and promotion

Since PHP 8.1 a property can be readonly — set once, inside the class, and never changed again. With constructor property promotion it is a single line:

public function __construct(private readonly OrderRepository $orders) {}

PHP 8.4 — asymmetric visibility

public private(set) string $status — anyone can read it, but only the class can write it. PHP 8.4 also brought property hooks — get/set logic directly on the property.

Invokable classes and controllers

A class with an __invoke() method can be called like a function: $action($order).

A single action controller is a controller with just one action — php artisan make:controller PayOrder --invokable. The route takes only the class name.

Keep reading