Skip to content

Instantly share code, notes, and snippets.

@Dinslinger90
Forked from messified/php-interview.md
Created September 7, 2019 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dinslinger90/f5bc4b2e8a47085f6908c46066c54046 to your computer and use it in GitHub Desktop.
Save Dinslinger90/f5bc4b2e8a47085f6908c46066c54046 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

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