45 Important PHP Interview Questions That May Land You a Job

Alex πŸ‘¨πŸΌβ€πŸ’»FullStack.Cafe - Sep 13 '19 - - Dev Community

45 Important PHP Interview Questions That May Land You a Job
The average PHP Developer salary in Australia is $95,000 per year or $48.72 per hour. Entry level positions start at $55,000 per year while most experienced workers make up to $161,500 per year. Follow along and learn 45 important PHP interview questions that may land you a job.

Originally published on FullStack.Cafe - Never Fail Your Tech Interview Again

Q1: What is the difference between == and ===?

Topic: PHP
Difficulty: ⭐

  • The operator == casts between two different types if they are different
  • The === operator performs a 'typesafe comparison'

That means that it will only return true if both operands have the same type and the same value.

1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // "1" gets casted to an integer, which is 1
"foo" === "foo": true // both operands are strings and have the same value
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: stackoverflow.com

Q2: How can you pass a variable by reference?

Topic: PHP
Difficulty: ⭐

To be able to pass a variable by reference, we use an ampersand in front of it, as follows:

$var1 = &$var2
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: guru99.com

Q3: What does $GLOBALS mean?

Topic: PHP
Difficulty: ⭐

$GLOBALS is associative array including references to all variables which are currently defined in the global scope of the script.

πŸ”— Source: guru99.com

Q4: What is the use of ini_set()?

Topic: PHP
Difficulty: ⭐

PHP allows the user to modify some of its settings mentioned in php.ini using ini_set(). This function requires two string arguments. First one is the name of the setting to be modified and the second one is the new value to be assigned to it.

Given line of code will enable the display_error setting for the script if it’s disabled.

ini_set('display_errors', '1');

We need to put the above statement, at the top of the script so that, the setting remains enabled till the end. Also, the values set via ini_set() are applicable, only to the current script. Thereafter, PHP will start using the original values from php.ini.

πŸ”— Source: github.com/Bootsity

Q5: When should I use require vs. include?

Topic: PHP
Difficulty: ⭐⭐

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

My suggestion is to just use require_once 99.9% of the time.

Using require or include instead implies that your code is not reusable elsewhere, i.e. that the scripts you're pulling in actually execute code instead of making available a class or some function libraries.

πŸ”— Source: stackoverflow.com

Q6: What is stdClass in PHP?

Topic: PHP
Difficulty: ⭐⭐

stdClass is just a generic 'empty' class that's used when casting other types to objects. stdClass is not the base class for objects in PHP. This can be demonstrated fairly easily:

class Foo{}
$foo = new Foo();
echo ($foo instanceof stdClass)?'Y':'N'; // outputs 'N'
Enter fullscreen mode Exit fullscreen mode

It is useful for anonymous objects, dynamic properties, etc.

An easy way to consider the StdClass is as an alternative to associative array. See this example below that shows how json_decode() allows to get an StdClass instance or an associative array.
Also but not shown in this example, SoapClient::__soapCall returns an StdClass instance.

//Example with StdClass
$json = '{ "foo": "bar", "number": 42 }';
$stdInstance = json_decode($json);

echo $stdInstance - > foo.PHP_EOL; //"bar"
echo $stdInstance - > number.PHP_EOL; //42

//Example with associative array
$array = json_decode($json, true);

echo $array['foo'].PHP_EOL; //"bar"
echo $array['number'].PHP_EOL; //42
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: stackoverflow.com

Q7: What are the differences between die() and exit() functions in PHP?

Topic: PHP
Difficulty: ⭐⭐

There's no difference - they are the same. The only advantage of choosing die() over exit(), might be the time you spare on typing an extra letter.

πŸ”— Source: stackoverflow.com

Q8: What are the main differences between those

Topic: PHP
Difficulty: ⭐⭐

The fundamental difference between const vs define is that const defines constants at compile time, whereas define defines them at run time.

const FOO = 'BAR';
define('FOO', 'BAR');

// but
if (...) {
    const FOO = 'BAR';    // Invalid
}
if (...) {
    define('FOO', 'BAR'); // Valid
}
Enter fullscreen mode Exit fullscreen mode

Also until PHP 5.3, const could not be used in the global scope. You could only use this from within a class. This should be used when you want to set some kind of constant option or setting that pertains to that class. Or maybe you want to create some kind of enum. An example of good const usage is to get rid of magic numbers.

Define can be used for the same purpose, but it can only be used in the global scope. It should only be used for global settings that affect the entire application.

Unless you need any type of conditional or expressional definition, use consts instead of define()- simply for the sake of readability!

πŸ”— Source: stackoverflow.com

Q9: What's the difference between isset() and array_key_exists()?

Topic: PHP
Difficulty: ⭐⭐

  • array_key_exists will tell you if a key exists in an array and complains when $a does not exist.
  • isset will only return true if the key/variable exists and is not null. isset doesn't complain when $a does not exist.

Consider:

$a = array('key1' => 'Foo Bar', 'key2' => null);

isset($a['key1']);             // true
array_key_exists('key1', $a);  // true

isset($a['key2']);             // false
array_key_exists('key2', $a);  // true
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: stackoverflow.com

Q10: What is the difference between var_dump() and print_r()?

Topic: PHP
Difficulty: ⭐⭐

  • The var_dump function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.

  • The print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.

Consider:

$obj = (object) array('qualitypoint', 'technologies', 'India');
Enter fullscreen mode Exit fullscreen mode

var_dump($obj)will display below output in the screen:

object(stdClass)#1 (3) {
 [0]=> string(12) "qualitypoint"
 [1]=> string(12) "technologies"
 [2]=> string(5) "India"
}
Enter fullscreen mode Exit fullscreen mode

And, print_r($obj) will display below output in the screen.

stdClass Object ( 
 [0] => qualitypoint
 [1] => technologies
 [2] => India
)
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: stackoverflow.com

Q11: Explain what the different PHP errors are

Topic: PHP
Difficulty: ⭐⭐

  • A notice is a non-critical error saying something went wrong in execution, something minor like an undefined variable.
  • A warning is given when a more critical error like if an include() command went to retrieve a non-existent file. In both this and the error above, the script would continue.
  • A fatal error would terminate the code. Failure to satisfy a require() would generate this type of error, for example.

πŸ”— Source: pangara.com

Q12: How can you enable error reporting in PHP?

Topic: PHP
Difficulty: ⭐⭐

Check if β€œdisplay_errors” is equal β€œon” in the php.ini or declare β€œini_set('display_errors', 1)” in your script.

Then, include β€œerror_reporting(E_ALL)” in your code to display all types of error messages during the script execution.

πŸ”— Source: codementor.io

Q13: Declare some function with default parameter

Topic: PHP
Difficulty: ⭐⭐

Consider:

function showMessage($hello = false){
  echo ($hello) ? 'hello' : 'bye';
}
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: codementor.io

Q14: Is multiple inheritance supported in PHP?

Topic: PHP
Difficulty: ⭐⭐

PHP supports only single inheritance; it means that a class can be extended from only one single class using the keyword 'extended'.

πŸ”— Source: guru99.com

Q15: In PHP, objects are they passed by value or by reference?

Topic: PHP
Difficulty: ⭐⭐

In PHP, objects passed by value.

πŸ”— Source: guru99.com

Q16: What is the differences between $a != $b and $a !== $b?

Topic: PHP
Difficulty: ⭐⭐

!= means inequality (TRUE if $a is not equal to $b) and !== means non-identity (TRUE if $a is not identical to $b).

πŸ”— Source: guru99.com

Q17: What is PDO in PHP?

Topic: PHP
Difficulty: ⭐⭐

PDO stands for PHP Data Object.

It is a set of PHP extensions that provide a core PDO class and database, specific drivers. It provides a vendor-neutral, lightweight, data-access abstraction layer. Thus, no matter what database we use, the function to issue queries and fetch data will be same. It focuses on data access abstraction rather than database abstraction.

πŸ”— Source: github.com/Bootsity

Q18: Explain how we handle exceptions in PHP?

Topic: PHP
Difficulty: ⭐⭐

When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception".
An exception can be thrown, and caught within PHP.

To handle exceptions, code may be surrounded in a try block.
Each try must have at least one corresponding catch block. Multiple catch blocks can be used to catch different classes of exceptions.
Exceptions can be thrown (or re-thrown) within a catch block.

Consider:

try {
    print "this is our try block n";
    throw new Exception();
} catch (Exception $e) {
    print "something went wrong, caught yah! n";
} finally {
    print "this part is always executed n";
}
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: github.com/Bootsity

Q19: Differentiate between echo and print()

Topic: PHP
Difficulty: ⭐⭐

echo and print are more or less the same. They are both used to output data to the screen.

The differences are:

  • echo has no return value while print has a return value of 1 so it can be used in expressions.
  • echo can take multiple parameters (although such usage is rare) while print can take one argument.
  • echo is faster than print.

πŸ”— Source: github.com/Bootsity

Q20: When should I use require_once vs. require?

Topic: PHP
Difficulty: ⭐⭐⭐

The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

My suggestion is to just use require_once 99.9% of the time.

Using require or include instead implies that your code is not reusable elsewhere, i.e. that the scripts you're pulling in actually execute code instead of making available a class or some function libraries.

πŸ”— Source: stackoverflow.com

Q21: Check if PHP array is associative

Topic: PHP
Difficulty: ⭐⭐⭐

Consider:

function has_string_keys(array $array) {
  return count(array_filter(array_keys($array), 'is_string')) > 0;
}
Enter fullscreen mode Exit fullscreen mode

If there is at least one string key, $array will be regarded as an associative array.

πŸ”— Source: stackoverflow.com

Q22: How do I pass variables and data from PHP to JavaScript?

Topic: PHP
Difficulty: ⭐⭐⭐

There are actually several approaches to do this:

  • Use AJAX to get the data you need from the server. Consider get-data.php:
echo json_encode(42);
Enter fullscreen mode Exit fullscreen mode

Consider index.html:

<script>
    function reqListener () {
      console.log(this.responseText);
    }

    var oReq = new XMLHttpRequest(); // New request object
    oReq.onload = function() {
        // This is where you handle what to do with the response.
        // The actual data is found on this.responseText
        alert(this.responseText); // Will alert: 42
    };
    oReq.open("get", "get-data.php", true);
    //                               ^ Don't block the rest of the execution.
    //                                 Don't wait until the request finishes to
    //                                 continue.
    oReq.send();
</script>
Enter fullscreen mode Exit fullscreen mode
  • Echo the data into the page somewhere, and use JavaScript to get the information from the DOM.
<div id="dom-target" style="display: none;">
    <?php
        $output = "42"; // Again, do some operation, get the output.
        echo htmlspecialchars($output); /* You have to escape because the result
                                           will not be valid HTML otherwise. */
    ?>
</div>
<script>
    var div = document.getElementById("dom-target");
    var myData = div.textContent;
</script>
Enter fullscreen mode Exit fullscreen mode
  • Echo the data directly to JavaScript.
<script>
    var data = <?php echo json_encode("42", JSON_HEX_TAG); ?>; // Don't forget the extra semicolon!
</script>
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: stackoverflow.com

Q23: Is there a function to make a copy of a PHP array to another?

Topic: PHP
Difficulty: ⭐⭐⭐

In PHP arrays are assigned by copy, while objects are assigned by reference so PHP will copy the array by default. References in PHP have to be explicit:

$a = array(1,2);
$b = $a; // $b will be a different array
$c = &$a; // $c will be a reference to $a
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: stackoverflow.com

Q24: What will be returned by this code?

Topic: PHP
Difficulty: ⭐⭐⭐

Consider the code:

$a = new stdClass();
$a->foo = "bar";
$b = clone $a;
var_dump($a === $b);
Enter fullscreen mode Exit fullscreen mode

What will be echoed to the console?


Two instances of the same class with equivalent members do NOT match the === operator. So the answer is:

bool(false)
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: stackoverflow.com

Q25: What will be returned by this code? Explain the result.

Topic: PHP
Difficulty: ⭐⭐⭐

Consider the code. What will be returned as a result?

$something = 0;
echo ('password123' == $something) ? 'true' : 'false';
Enter fullscreen mode Exit fullscreen mode

The answer is true. You should never use == for string comparison. Even if you are comparing strings to strings, PHP will implicitly cast them to floats and do a numerical comparison if they appear numerical. === is OK.

For example

'1e3' == '1000' // true
Enter fullscreen mode Exit fullscreen mode

also returns true.

πŸ”— Source: stackoverflow.com

Q26: What exactly is the the difference between array_map, array_walk and array_filter?

Topic: PHP
Difficulty: ⭐⭐⭐

  • array_walk takes an array and a function F and modifies it by replacing every element x with F(x).
  • array_map does the exact same thing except that instead of modifying in-place it will return a new array with the transformed elements.
  • array_filter with function F, instead of transforming the elements, will remove any elements for which F(x) is not true

πŸ”— Source: stackoverflow.com

Q27: Explain the difference between exec() vs system() vs passthru()?

Topic: PHP
Difficulty: ⭐⭐⭐

  • exec() is for calling a system command, and perhaps dealing with the output yourself.
  • system() is for executing a system command and immediately displaying the output - presumably text.
  • passthru() is for executing a system command which you wish the raw return from - presumably something binary.

πŸ”— Source: stackoverflow.com

Q28: How would you create a Singleton class using PHP?

Topic: PHP
Difficulty: ⭐⭐⭐

/**
 * Singleton class
 *
 */
final class UserFactory {
    /**
     * Call this method to get singleton
     *
     * @return UserFactory
     */
    public static
    function Instance() {
        static $inst = null;
        if ($inst === null) {
            $inst = new UserFactory();
        }
        return $inst;
    }

    /**
     * Private ctor so nobody else can instantiate it
     *
     */
    private
    function __construct() {

    }
}
Enter fullscreen mode Exit fullscreen mode

To use:

$fact = UserFactory::Instance();
$fact2 = UserFactory::Instance();
Enter fullscreen mode Exit fullscreen mode

But:

$fact = new UserFactory()
Enter fullscreen mode Exit fullscreen mode

Throws an error.

πŸ”— Source: stackoverflow.com

Q29: What is the difference between PDO's query() vs execute()?

Topic: PHP
Difficulty: ⭐⭐⭐

  • query runs a standard SQL statement and requires you to properly escape all data to avoid SQL Injections and other issues.
  • execute runs a prepared statement which allows you to bind parameters to avoid the need to escape or quote the parameters. execute will also perform better if you are repeating a query multiple times.

Best practice is to stick with prepared statements and execute for increased security. Aside from the escaping on the client-side that it provides, a prepared statement is compiled on the server-side once, and then can be passed different parameters at each execution.

πŸ”— Source: stackoverflow.com

Q30: What is use of Null Coalesce Operator?

Topic: PHP
Difficulty: ⭐⭐⭐

Null coalescing operator returns its first operand if it exists and is not NULL. Otherwise it returns its second operand.

Example:

$name = $firstName ?? $username ?? $placeholder ?? "Guest"; 
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: github.com/Bootsity

Q31: Differentiate between exception and error

Topic: PHP
Difficulty: ⭐⭐⭐

  • Recovering from Error is not possible. The only solution to errors is to terminate the execution. Where as you can recover from Exception by using either try-catch blocks or throwing exception back to caller.
  • You will not be able to handle the Errors using try-catch blocks. Even if you handle them using try-catch blocks, your application will not recover if they happen. On the other hand, Exceptions can be handled using try-catch blocks and can make program flow normal if they happen.
  • Exceptions are related to application where as Errors are related to environment in which application is running.

πŸ”— Source: github.com/Bootsity

Q32: What are the exception class functions?

Topic: PHP
Difficulty: ⭐⭐⭐

There are following functions which can be used from Exception class.

  • getMessage() βˆ’ message of exception
  • getCode() βˆ’ code of exception
  • getFile() βˆ’ source filename
  • getLine() βˆ’ source line
  • getTrace() βˆ’ n array of the backtrace()
  • getTraceAsString() βˆ’ formated string of trace
  • Exception::__toString gives the string representation of the exception.

πŸ”— Source: github.com/Bootsity

Q33: Differentiate between parameterised and non parameterised functions

Topic: PHP
Difficulty: ⭐⭐⭐

  • Non parameterised functions don't take any parameter at the time of calling.
  • Parameterised functions take one or more arguments while calling. These are used at run time of the program when output depends on dynamic values given at run time There are two ways to access the parameterised function:
    1. call by value: (here we pass the value directly )
    2. call by reference: (here we pass the address location where the value is stored)

πŸ”— Source: github.com/Bootsity

Q34: Explain function call by reference

Topic: PHP
Difficulty: ⭐⭐⭐

In case of call by reference, actual value is modified if it is modified inside the function. In such case, we need to use & symbol with formal arguments. The & represents reference of the variable.

Example:

function adder(&$str2) {  
    $str2 .= 'Call By Reference';  
}
$str = 'This is ';  
adder($str);  
echo $str;  
Enter fullscreen mode Exit fullscreen mode

Output:

This is Call By Reference
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: github.com/Bootsity

Q35: Why do we use extract()?

Topic: PHP
Difficulty: ⭐⭐⭐

The extract() function imports variables into the local symbol table from an array.
This function uses array keys as variable names and values as variable values. For each element it will create a variable in the current symbol table.
This function returns the number of variables extracted on success.

Example:

$a = "Original";
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
Enter fullscreen mode Exit fullscreen mode

Output:

$a = Cat; $b = Dog; $c = Horse
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: github.com/Bootsity

Q36: explain what is a closure in PHP and why does it use the β€œuse” identifier?

Topic: PHP
Difficulty: ⭐⭐⭐⭐

Consider this code:

public function getTotal($tax)
{
    $total = 0.00;

    $callback =
        function ($quantity, $product) use ($tax, &$total)
        {
            $pricePerItem = constant(__CLASS__ . "::PRICE_" .
                strtoupper($product));
            $total += ($pricePerItem * $quantity) * ($tax + 1.0);
        };

    array_walk($this->products, $callback);
    return round($total, 2);
}
Enter fullscreen mode Exit fullscreen mode

Could you explain why use it?


This is how PHP expresses a closure. Basically what this means is that you are allowing the anonymous function to "capture" local variables (in this case, $tax and a reference to $total) outside of it scope and preserve their values (or in the case of $total the reference to $total itself) as state within the anonymous function itself.

A closure is a separate namespace, normally, you can not access variables defined outside of this namespace.

  • use allows you to access (use) the succeeding variables inside the closure.
  • use is early binding. That means the variable values are COPIED upon DEFINING the closure. So modifying $tax inside the closure has no external effect, unless it is a pointer, like an object is.
  • You can pass in variables as pointers like in case of &$total. This way, modifying the value of $total DOES HAVE an external effect, the original variable's value changes.

πŸ”— Source: stackoverflow.com

Q37: What exactly are late static bindings in PHP?

Topic: PHP
Difficulty: ⭐⭐⭐⭐

Basically, it boils down to the fact that the self keyword does not follow the same rules of inheritance. self always resolves to the class in which it is used. This means that if you make a method in a parent class and call it from a child class, self will not reference the child as you might expect.

Late static binding introduces a new use for the static keyword, which addresses this particular shortcoming. When you use static, it represents the class where you first use it, ie. it 'binds' to the runtime class.

Consider:

class Car {
    public static
    function run() {
        return static::getName();
    }

    private static
    function getName() {
        return 'Car';
    }
}

class Toyota extends Car {
    public static
    function getName() {
        return 'Toyota';
    }
}

echo Car::run(); // Output: Car
echo Toyota::run(); // Output: Toyota
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: stackoverflow.com

Q38: How to measure execution times of PHP scripts?

Topic: PHP
Difficulty: ⭐⭐⭐⭐

I want to know how many milliseconds a PHP while-loop takes to execute. Could you help me?


You can use the microtime function for this.

Consider:

$start = microtime(true);
while (...) {

}
$time_elapsed_secs = microtime(true) - $start;
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: stackoverflow.com

Q39: What is the best method to merge two PHP objects?

Topic: PHP
Difficulty: ⭐⭐⭐⭐

//We have this:
$objectA->a;
$objectA->b;
$objectB->c;
$objectB->d;

//We want the easiest way to get:
$objectC->a;
$objectC->b;
$objectC->c;
$objectC->d;
Enter fullscreen mode Exit fullscreen mode

This works:

$obj_merged = (object) array_merge((array) $obj1, (array) $obj2);
Enter fullscreen mode Exit fullscreen mode

You may also use array_merge_recursive to have a deep copy behavior.

One more way to do that is:

foreach($objectA as $k => $v) $objectB->$k = $v;
Enter fullscreen mode Exit fullscreen mode

This is faster than the first answer in PHP versions < 7 (estimated 50% faster). But in PHP >= 7 the first answer is something like 400% faster.

πŸ”— Source: stackoverflow.com

Q40: Compare mysqli or PDO - what are the pros and cons?

Topic: PHP
Difficulty: ⭐⭐⭐⭐

Let's name some:

  • PDO is the standard, it's what most developers will expect to use.

  • Moving an application from one database to another isn't very common, but sooner or later you may find yourself working on another project using a different RDBMS. If you're at home with PDO then there will at least be one thing less to learn at that point.

  • A really nice thing with PDO is you can fetch the data, injecting it automatically in an object.

  • PDO has some features that help agains SQL injection

  • In sense of speed of execution MySQLi wins, but unless you have a good wrapper using MySQLi, its functions dealing with prepared statements are awful. inserts - almost equal, selects - mysqli is ~2.5% faster for non-prepared statements/~6.7% faster for prepared statements.

πŸ”— Source: stackoverflow.com

Q41: What is use of Spaceship Operator?

Topic: PHP
Difficulty: ⭐⭐⭐⭐

This <=> operator will offer combined comparison in that it will:

  • Return 0 if values on either side are equal
  • Return 1 if value on the left is greater
  • Return -1 if the value on the right is greater

Consider:

//Comparing Integers
echo 1 <= > 1; //outputs 0
echo 3 <= > 4; //outputs -1
echo 4 <= > 3; //outputs 1

//String Comparison

echo "x" <= > "x"; // 0
echo "x" <= > "y"; //-1
echo "y" <= > "x"; //1
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: github.com/Bootsity

Q42: Does PHP have threading?

Topic: PHP
Difficulty: ⭐⭐⭐⭐

Standard php does not provide any multithreading but there is an (experimental) extension that actually does - pthreads. The next best thing would be to simply have one script execute another via CLI, but that's a bit rudimentary. Depending on what you are trying to do and how complex it is, this may or may not be an option.

πŸ”— Source: github.com/Bootsity

Q43: Is PHP single or multi threaded?

Topic: PHP
Difficulty: ⭐⭐⭐⭐

PHP is not single threaded by nature. It is, however, the case that the most common installation of PHP on unix systems is a single threaded setup, as is the most common Apache installation, and nginx doesn't have a thread based architecture whatever. In the most common Windows setup and some more advanced unix setups, PHP can and does operate multiple interpreter threads in one process.

PHP as an interpreter had support for multi-threading since the year 2000.

πŸ”— Source: github.com/Bootsity

Q44: Provide some ways to mimic multiple constructors in PHP

Topic: PHP
Difficulty: ⭐⭐⭐⭐⭐

It's known you can't put two __construct functions with unique argument signatures in a PHP class but I'd like to do something like this:

class Student 
{
   protected $id;
   protected $name;
   // etc.

   public function __construct($id){
       $this->id = $id;
      // other members are still uninitialised
   }

   public function __construct($row_from_database){
       $this->id = $row_from_database->id;
       $this->name = $row_from_database->name;
       // etc.
   }
}
Enter fullscreen mode Exit fullscreen mode

What is the best way to achieve this in PHP?


I'd probably do something like this:

class Student
{
    public function __construct() {
        // allocate your stuff
    }

    public static function withID( $id ) {
        $instance = new self();
        $instance->loadByID( $id );
        return $instance;
    }

    public static function withRow( array $row ) {
        $instance = new self();
        $instance->fill( $row );
        return $instance;
    }

    protected function loadByID( $id ) {
        // do query
        $row = my_awesome_db_access_stuff( $id );
        $this->fill( $row );
    }

    protected function fill( array $row ) {
        // fill all properties from array
    }
}
Enter fullscreen mode Exit fullscreen mode

Then if i want a Student where i know the ID:

$student = Student::withID( $id );
Enter fullscreen mode Exit fullscreen mode

Technically you're not building multiple constructors, just static helper methods, but you get to avoid a lot of spaghetti code in the constructor this way.

Another way is to use the mix of factory and fluent style:

class Student
{
    protected $firstName;
    protected $lastName;
    // etc.

    /**
     * Constructor
     */
    public function __construct() {
        // allocate your stuff
    }

    /**
     * Static constructor / factory
     */
    public static function create() {
        $instance = new self();
        return $instance;
    }

    /**
     * FirstName setter - fluent style
     */
    public function setFirstName( $firstName) {
        $this->firstName = $firstName;
        return $this;
    }

    /**
     * LastName setter - fluent style
     */
    public function setLastName( $lastName) {
        $this->lastName = $lastName;
        return $this;
    }
}

// create instance
$student= Student::create()->setFirstName("John")->setLastName("Doe");
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: stackoverflow.com

Q45: How could we implement method overloading in PHP?

Topic: PHP
Difficulty: ⭐⭐⭐⭐⭐

You cannot overload PHP functions. Function signatures are based only on their names and do not include argument lists, so you cannot have two functions with the same name.

You can, however, declare a variadic function that takes in a variable number of arguments. You would use func_num_args() and func_get_arg() to get the arguments passed, and use them normally.

Consider:

function myFunc() {
    for ($i = 0; $i < func_num_args(); $i++) {
        printf("Argument %d: %s\n", $i, func_get_arg($i));
    }
}

/*
Argument 0: a
Argument 1: 2
Argument 2: 3.5
*/
myFunc('a', 2, 3.5);
Enter fullscreen mode Exit fullscreen mode

πŸ”— Source: github.com/Bootsity

Thanks πŸ™Œ for reading and good luck on your interview!
Please share this article with your fellow devs if you like it!
Check more FullStack Interview Questions & Answers on πŸ‘‰ www.fullstack.cafe

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .