Technical PHP Questions
PHP Technical online Test

When designing a new class, what visibility do you select for its members unless otherwise required (explain your choice):

Name one scenario where using === / !== is a necessity. Is it always preferable to use === over == ?

The following piece of code is not ideal. Explain what the problem is and what you could do to fix it:

$a = [
  0 => 10,
];
$i = 0;

$a[$i++] = $i;

The following piece of code is not ideal. Explain what the problem is and what you could do to fix it:

$a = true;
$b = false;

echo $a ? 'a' : $b ? 'b' : 'c';

The following piece of code is not ideal. Explain what the problem is and what you could do to fix it:

function getAdminName() {
  global $admin;
  return ($admin['gender'] ? 'Mr' : 'Mrs').' '.$admin['fullname'];
}

Provide and explain the expected output of the following piece of code: (assume latest PHP 7 version)

<?php

class A {
  public static $name = 'A';

  public static function getName() {
    return self::$name;
  }

  public static function test1() {
    echo self::getName()."\n";
  }
  public function test2() {
    echo $this->getName()."\n";
  }
  public function test3() {
    echo $this::getName()."\n";
  }
}

class B extends A {
  public static $name = 'B';

  public static function getName() {
    return self::$name;
  }
}

echo "Test 1:\n";
A::test1();
B::test1();

$a = new A();
$b = new B();
echo "Test 2:\n";
$a->test2();
$b->test2();

echo "Test 3:\n";
$a->test3();
$b->test3();

Provide and explain the expected output of the following piece of code: (assume latest PHP 7 version)

class A {
  public $name = 'A';

  public function test($a) {
    $f = $a->get();
    $f();
  }

  public function get() {
    return function() {
      echo $this->name."\n";
    };
  }
}

class B extends A {
  public $name = 'B';
}

$a = new A();
$b = new B();

echo "Test 1:\n";
$a->test($a);
$b->test($b);

echo "Test 2:\n";
$a->test($b);
$b->test($a);

Please provide a code-scenario for which ::class constants are useful

Provide one advantage and one drawback of code relying on magic methods such as __get / __set or __call