Skip to content
December Code

PHP · 8 topic pages · 48 questions in the bank

PHP for placements: interview questions by topic

PHP still runs a large share of the web, from WordPress sites to Laravel and Symfony applications, and service companies and web agencies hire for it in every placement season. PHP interviews have a reputation for trick questions, and there is a reason: the language converts types freely, its arrays do the work of several data structures, and some of its oldest behaviour changed in PHP 8. A candidate who learned from old tutorials can give answers that were right in 2015 and are wrong today.

The questions therefore test the rules under the syntax: when == converts and when === does not, how arrays are copied and merged, how closures capture variables, what self, static and parent refer to, how traits and interfaces combine, and which failures are Exceptions and which are Errors. Modern features such as match, named arguments and constructor promotion come up too, because they show whether someone writes current PHP.

The pages below take one topic each, with answers whose code was run on PHP 8.5, and one question exactly as the diagnostic asks it. The diagnostic draws from every PHP topic in the bank and names the ones to read first.

New to this? How to prepare for placements — the method — and what the readiness test measures.

On this page3 sections

PHP topics, one page each

One question, exactly as the diagnostic asks it

From the PHP bank — 48 questions across 8 topics. The answer is marked because this one is public; in a sitting you choose first, then see why each option is right or wrong.

Classes & Objects · mediumPHP-016

What does this PHP code print?

PHP
class A {
    public static function make() {
        return new static();
    }
    public static function makeSelf() {
        return new self();
    }
}
class B extends A {}
echo get_class(B::make()), " ", get_class(B::makeSelf());
  1. 1

    A A

  2. 2

    B B

  3. 3

    B A

    correct
  4. 4

    A B

PHP
self always refers to the class in which the method is written, A, so new self() creates an A even when called as B::makeSelf(). static is resolved at run time to the class the method was called on, which is late static binding: B::make() runs A's make with static meaning B, so it creates a B. The output is B A. A A treats static like self. B B treats self like static. A B has them the wrong way round. new static is how factory methods in a base class build the right subclass.

More on this topic: PHP OOP interview questions

Measure it

Reading the answers tells you what’s true. A diagnostic tells you what you get wrong.

10 PHP questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: a diagnostic in every subject, a retake a week and 15 problems a day. No card.

By Harshit · updated