PHP Developer Interview Questions

Name the Four Fundamental Principles of OOP?
What is the difference between PUBLIC, PRIVATE AND PROTECTED
  • 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.
  • Manual

Describe the difference from an array and an associative array?
Arrays in PHP are really “maps” not the strict arrays you find in other languages. The main difference is that you define the indexes in the associative array. While when you do not define the indexes are defined by the system starting at 0 and incremented by 1. Manual
Describe the difference between double equals (==) and triple equals (===)
Directly from the manual:
$a == $b Equal TRUE if $a is equal to $b after type juggling.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.


//Type comparison (Bool vs Int):
var_dump((false == 0)); //true
var_dump((true === 1)); //false

//Type comparison (String vs Int):
var_dump(("0" == 0)); //true
var_dump(("1" === 1)); //false

//Object comparison
// __toString is not compared
$t1 = new Test();
$t2 = new Test();

var_dump(($t1 == $t2)); //True
var_dump(($t1 === $t2)); //False

$t1->val = "yes";
$t2->val = "yes";

var_dump(($t1 == $t2)); //True
var_dump(($t1 === $t2)); //False

$t1->val = "yes";
$t2->val = "no";

var_dump(($t1 == $t2)); //False
var_dump(($t1 === $t2)); //False
Give an example of using GROUP BY in MySQL
-- Count orders by zip code
SELECT `zip`, COUNT(`zip`)
FROM orders
GROUP BY `zip`;
Explain the difference between WHERE and HAVING in MySQL?
They function similarly, but their uses differ. The WHERE statement is used to filter field values that are in the table. The HAVING statement is used to filter calculated values, like sum().

-- WHERE query
SELECT `order_number`, `state`
FROM orders
WHERE `created_at` > '2014-01-01';
-- Returns all orders created after 2014-01-01

-- HAVING query
SELECT `invoicenumber`, SUM(`amount`) as `total`
FROM invoices
GROUP BY `invoicenumber`
HAVING `total` > 500;
-- Returns all invoices that have a total amount over 500.

CakePHP set CreatedAt field on save

So after a quick search I found that CakePHP automatically updates ‘created’ and ‘modified’ for you. Well the database I already had setup uses the fields ‘CreatedAt’ and ‘UpdatedAt’ instead. I didn’t want to go through and modify almost every table in the database just so I don’t have to set the CreatedAt date manually with every new record.

So to make sure I get CreatedAt set on new records I added this to my AppModel.php

public function beforeSave($options = array()) {
        if(!isset($this->data[$this->alias]['id'])) {
                $this->data[$this->alias]['CreatedAt'] = date("Y/m/d H:i:s");

Before the data is actually saved and if there is no id, since new records wouldn’t have one yet, it sets ‘CreatedAt’ to the current time. If there is a way to change ‘created’ to ‘CreatedAt’ I would prefer to do that, but this is working for me at the moment.

Because ‘UpdatedAt’ is set to update to the current timestamp on update through the database, I don’t have to worry about CakePHP doing it for me.

PHP, Perl, Apache, Dancer, Starman and Beanstalkd – When our powers combine!

That is a mouthful, it almost sounds like a super hero team roster.  In fact I think Apache and Starman are super heroes, or at least are in comics.

So what am I doing with all of these things?  I am planning on using them together for a new side project I am working on.  At first glance it doesn’t seem to make sense, I mean who would choose to use two different languages (PHP, Perl) and two different web servers (Apache, Starman)?  Why would I not just use PHP and Apache or use Perl/Dancer and Starman.  The reason is speed of coding mainly, PHP is my primary language, then after that is Perl and third is Java but that’s not related to this.  So the mashing of services to is to provide a quick yet robust solution while also challenging me enough to learn some new things to keep my interest.

The Break Down:

  • PHP & Apache will handle the main site and crud
  • Apache will proxy out to Starman (this will allow for easier separation when needed)
  • Perl using Dancer and Starman will handle the incoming API calls; validating, parsing and pushing the result to beanstalkd
  • Beanstalkd workers will most likely written in PHP to leverage the framework I’m using, but can be written in Perl

Breaking that out is not really convincing that running the API with Perl, Dancer and Starman is the right thing when it’s all on the same server.  An advantage of doing this is mitigating my mistakenly exposing anything since it doesn’t talk to the main PHP site directly.  Makes a little more sense when we separate the API out, I wouldn’t want to have to replicate the PHP server for simple things the API is doing.

Project links: PHP, PerlApache, Dancer, Starman, Beanstalkd