Skip to content

Instantly share code, notes, and snippets.

@messified
Last active August 8, 2023 12:03
Show Gist options
  • Star 67 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save messified/6381844 to your computer and use it in GitHub Desktop.
Save messified/6381844 to your computer and use it in GitHub Desktop.
PHP Engineer Interview: What you should know

PHP Developer Interview: What you should know

1. What’s the difference between " self " and " this " ?

Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members.

Source: When to use self vs this -stackoverflow

2. What is a " Static Class "? Why use it?

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

Source: Static Class -PHP Manual

One thing about static methods/classes is that they don't facilitate unit testing (at least in PHP, but probably in other languages too). Another thing about static data is that only one instance of it exists in your program : if you set MyClass::$myData to some value somewhere, it'll have this value, and only it, everywhere -- Speaking about the user, you would be able to have only one user -- which is not that great, is it ?

Source: When to use static -stackoverflow

3. What is a Closure?

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses.

Closures can also be used as the values of variables; PHP automatically converts such expressions into instances of the Closure internal class. Assigning a closure to a variable uses the same syntax as any other assignment, including the trailing semicolon :

Source: Anonymous functions -PHP Manual

4. Describe the difference between Public, Protected, and Private.

  • Public scope to make that variable/function available from anywhere, other classes and instances of the object.
  • Private scope when you want your variable/function to be visible in its own class only.
  • Protected scope when you want to make your variable/function visible in all classes that extend current class including the parent class.

Source: Public, Private, Protected -stackoverflow

5. What is dependency injection? Why is used? How does it relate to Composition?

Basically, instead of having your objects creating a dependency or asking a factory object to make one for them, you pass the needed dependencies in to the constructor, and you make it somebody else's problem (an object further up the dependency graph, or a dependency injector that builds the dependency graph). A dependency as I'm using it here is any other object the current object needs to hold a reference to.

Source: What is Dependency Injection? -stackoverflow

Checkout this great article on Dependency Injection.

Source: Dependency Injection Huh? -nettutsplus

Composition is an important concept in PHP. It occurs when an object creates another object; that is, the first object completely possesses the second object.

Source: Composition OOP

6. What does “ =& “ mean, and what kind of operator is it?

Passing by Reference

Assignment Operators -PHP Manual

Operators, PHP OPERATORS -w3schools

7. Explain Abstract Class?

You could, naturally. However, if there are many objects that are of pretty much the same type, it might help to extract common functionality out into a "base" class, which means you don't have to duplicate that logic.

Source: Abstract class in php -stackoverflow

8. What is a natural join? What is a Left or Right join?

Resource - SQL JOINS -w3schools

9. Common Fizz Buzz problem.

/**
 * FIZZ BUZZ
 *
 * Write a program that prints the numbers from 1 to 100. But for multiples of three
 * print "Fizz" instead of the number and for the multiples of five print "Buzz".
 * For numbers which are multiples of both three and five print “FizzBuzz”.
 *
 *
 */

for($i = 1; $i <= 100; $i ++)
{
	if($i % 3 == 0 && $i % 5 == 0)
	{
		echo 'FizzBuzz';
	}
	else if($i % 3 == 0)
	{
		echo 'Fizz';
	}
	else if($i % 5 == 0)
	{
		echo 'Buzz';
	}
	else
	{
		echo $i;
	}

	echo '<br/>';
}

PHP: The Right Way - Now read everything from this source!

PHP The Right Way

@saurini
Copy link

saurini commented Aug 30, 2013

Other common questions include:

  • What is MVC? What does a (Model|View|Controller) do?
  • What are the differences between PHP 5.x and PHP 5.y?
  • What is your favorite design pattern? What others do you know?
  • What's the difference between print and echo?
  • How would you go about programming the Fibonacci sequence for n numbers?
  • What is IoC?

@carbontwelve
Copy link

Here are a few I have used in the past:

  • What are traits?
  • When are traits useful?
  • When is it best to use the factory patern?
  • How is Mockery useful in PHPUnit testing?
  • With regards to unit testing explain in detail your views on code coverage
  • Explain the fundamental difference between MongoDB and MySQL
  • What sort of application would benefit from using a MongoDB datastore over a MySQL one?
  • Are you aware of the PHP-FIG?
  • Given a large data store ( > 300MB) of data within a single table in a MySQL database, how would you parse through the data to generate stats without hitting the default PHP memory limit?
  • Imagine you have a cron job running every 5 minutes, the job in question sometimes takes up to 10 minutes or longer to run. Given this information write a method that would check at the start of the cron process if it is already running and shut down if it is. Sometimes the cron process crashes, your method must mitigate this.
  • On a data store what is an atomic event?
  • What is the difference between if ($a == $b & $c == $d) and if ($a == $b && $c == $d)

@judgej
Copy link

judgej commented Aug 30, 2013

FizzBuzz - you can take all those <br />s out of the condition statements and put just one at the end.

Recognising patterns to reduce duplication and recognising how to separate functional requirements (e.g. generate line content vs wrapping the line in its HTML tags) is an important skill. I'm not trying to be pedantic about how correct or neat it is, as there are a hundred ways of doing this that would work equally well, just pointing out what I, as an interviewer, would spot and question. It is all about leaving well maintainable code for the next person.

@xquizzes
Copy link

xquizzes commented May 5, 2015

You can check www.xquizzes.com/programming/PHP hope it's helpful :)

@sharadjaiswal1411
Copy link

Submit your PHP interview Questions post at All top interview Questions Website

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment