Category Archives: Zend Framework

Developer Interview Questions

I had to interview a couple of developers yesteday for the first time in my career.  Having had plently of interviews myself I thought i’d share with my handful of readers what I asked the candidates.

Our company is looking for a PHP / Frontend developer if you’re interested and can answer the majority of the questions on here, leave your name and address 🙂

Server side

  1. What version of PHP / MySQL are you most familiar with?

 

  1. What frameworks have you worked on and for how long?
    1. Did you write your own at any point?
    2. Template engines used
    3. WordPress experience
  1. i.      Themes / Plugins?
  2. OOP/Patterns:
    1. i.      Consuming which formats
    2. i.      What approach did you take? SOAP / REST etc.
    3. i.      Nulls.
    4. What’s your CL editor of choice?
    1. Zend Framework experience?
  1. What’s your setup?
    1. Dev environment / clients.
    2. Server set up
    3. Browser
    4. OS
    5. General – happier with a GUI or hardcore CL? Working alone or in a team?
  2. What’s the largest database you’ve worked with? #records.
    1. Performance issues and how they were handled.
    2. Slow queries / tweaking the DB – how would he go about solving these.
  3. What’s the highest volume of traffic any of the projects you’ve worked on has received?
    1. 1 million unique per month – benchmark.
  4. What you’re approach to caching.
    1. How would you serve dynamic content in a high trafficked environment? e.g. School Closures feeds / Now playing info
    2. Ever used Memcached?
    1. Give a couple of examples of the last time he used a design pattern and what for.
    2. Favourite / Most over used pattern?
  1. Dirty hacks!
    1. Give an example of when you’ve thrown something together you’re not proud of and what could you have done better?
  2. Web Services
    1. Give a couple examples of when you’ve worked with web services or API’s
    1. Are you happy working with XML and JSON?
    2. JSONP – what is it?
    3. Have you ever written any API’s?
  1. SQL Skills
    1. Happy writing SQL by hand?
    2. Use a GUI? PhpMyAdmin?
    3. Difference between a PK and a unique?
  1. Sys admin skills
    1. Happy copying / navigating files around on the command line?
    2. Ever deleted anything you shouldn’t have?
    3. Tailing logs? Do you know how?
  1. Version control
    1. Preferred client?
    2. Happy with command line access or prefer a GUI
  2. Regular expressions
    1. Love em or Hate em?

Client Side

  1. HTML skills
    1. Are you working with HTML 4 or 5
  1. i.      If HTML 5, give examples of the things you’ve done.
  2. i.      What other libraries have you used?
    1. YUI / jQuery / Ext / Prototype / Scriptaculous / Moo
  3. i.      Who are Douglas Crockford and John Resig?
  4. i.      Ever used sprites?
    1. Do you code by hand?
  1. Javascript
    1. How would you rate your core (without a framework JS) skills.
    2. What’s your preferred JS library?
    1. Are you happy writing OO JavaScript code?
  1. CSS / Design
    1. Happy to write CSS by hand?
    1. Photoshop / Design experience?
    2. CSS frameworks

 

General

  1. Name your favourite tech sites / blogs you regularly read.
  2. Development goals for this year
    1. Learn HTML 5, Cross Domain AJAX – Intelligent apps.
  3. Any side projects on the go?
  4. Have you ever built any intelligent applications?
    1. Recommendations / Classifiers etc?
  5. Basic aims/techniques of SEO?
  6. Cloud sites?
    1. Ever built any?
  7. Mobile development experience
    1. Ever written any iPhone or Android Apps?
  8. Facebook apps?
  9. Magento Experience?
  10. Facebook / Twitter / MySpace?

Zend_Db_Table_Row and Zend_Cache

Ever had the message that the Row you’ve retrived from Zend_Cache is not connected? Yet performing a var_dump on the row object shows it’s fully populated. The problem lies whe nthe row is unserialised from Zend_Cache. You must override the __wakeup() function in your row class and then set the table class.

THis is a breeze if you have an interim class extending Zend_Db_Table_Row whcih all your model row classes extend. But you could end up repeating yourself a lot if you’ve not.

Here’s the code:

<?php
class App_Db_AbstractRow extends Zend_Db_Table_Row
{
	public function __wakeup(){
		// tbl comes back as a string
		$tbl = $this->getTableClass();
		
		// dynamically instantiate an instance of the table
		$this->setTable( new $tbl);
	}
}

Zend Fw assumes unserialised data objects should not have access to live database data as stated in the documentation. Credit also Gustavo he also has a simlar solution.

Zend Multi Db

OK So i’ve been writing an app that make heavy use of Zend_Db, across multiple databases using the MultiDb plugin.
Here’s a rough guide of how to set it up.

Now 1st rule I have in any MVC project is to always extend Zend_Db_Table with some form of Abstract class which you then further extend. This gives you a space to provide common functionality across all of your models. So I’ve created 2 extra classes inside the library/App/Db folder and called them App_Db_AbstractTable and App_Db_AbstractRow respoectively. This way I can override any of the initiation logic from Zend for all model classes and provide common methods to all my models. Without these classes this code would need to be duplicated across all my models.

I created two separate connection strings in my config, which looks like this:

; database connection 1
resources.multidb.name1.adapter = PDO_MYSQL
resources.multidb.name1.dbname = 12312423_mydb
resources.multidb.name1.username = 12312423_user
resources.multidb.name1.password = "password"
resources.multidb.name1.host = "mysql50-xx.wc2.dfw1.stabletransit.com"
resources.multidb.name1.default = true

resources.multidb.name2.adapter = PDO_MYSQL
resources.multidb.name2.dbname = 444444_db2
resources.multidb.name2.username = 444444_dbuser2
resources.multidb.name2.password = "7213d8TShkd478"
resources.multidb.name2.host = "mysql50-xx.wc2.dfw1.stabletransit.com" - different cloud machine

In my bootstrap for the entire application I initialise the MultiDb plugin to store these adapters in the registry thus:

	protected function _initDbAdaptersToRegistry()
	{
		$this-&amp;gt;bootstrap('multidb');
		$resource = $this-&amp;gt;getPluginResource('multidb');
		$resource-&amp;gt;init();
				
		$Adapter1 = $resource-&amp;gt;getDb('name1');
		$Adapter2 = $resource-&amp;gt;getDb('name2');		
		Zend_Registry::set('Adapter1RegKey', $Adapter1);
		Zend_Registry::set('Adapter2RegKey',$Adapter2);
		
	}

In that method you can also stuff in some profiller initialisation code too if you use it etc etc.

Then in the App_Db_Abstract I setup the database adapter.

class Model_User extends App_Db_AbstractTable
{
	protected $_rowClass = 'User';
	
	protected $_name = 'table_name';
	
	protected $_schema = '';
	
	protected $_adapter = 'Adapter1RegKey';

I override the _setupDatabaseAdapter() method in the App_Db_AbstractTable to set the adapter as an object direct from the registry.

	protected function _setupDatabaseAdapter() 
	{
		$this-&amp;gt;_db = Zend_Registry::get($this-&amp;gt;_adapter);
	}

There’s MultiDb set up and working. Hope that helps.

Almost Perfect Rackspace Cloud .htaccess for PHP

Dear Reader(s?)

If you use Rackspace Cloud hosting you’ll recognise that thier PHP configuration is gash and you’ll be pulling your hair out trying to figure out why *some* ini_sets fail and sessions just don’t work at all, making you miserable. If you’re not and your hair is intact, good for you.

I learnt through trial and error so here’s what my current .htaccess file for Rackspace looks like

One thing to note is I use the Zend Framework alot, hence the write rules.

#Zend Framework Specific.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

php_value session.gc_probability 1
php_value session.gc_divisor 100
php_value session.gc_maxlifetime 3600
php_value session.save_path /mnt/stor1-wc2-dfw1/654321/12356/www.sitename.com/web/sessions

php_value max_execution_time 3600
php_value upload_max_filesize 100M
php_value post_max_size 220M
php_value memory_limit 256M
php_value magic_quotes_gpc Off

So bit sucky that you have to do that, but credit where credit is due, their live chat support is great. Instant answers to anything you can’t find in their cloud hosting wiki.

Another quality reference page to check is the .htaccess faqs page too, before you start buggin their support agents 😉

Zend_Route via Zend_Config_Ini

I recently made an app that required several modules and several routes in order to produce some sexy urls and avoid the usual Zend Framework url chaff like /index/myaction. I started adding these routes to the Bootstrap class of the main application (not in the individual module bootstrap to which the urls/routes point).

After realising I was making a stupidly large method for some rather basic functionality I read the Zend Docs and came up with the following solution:

Create a routes.ini in APPLICATION_DIR “/configs/routes.ini” or the location of your choice.

Then populate your routes file thus:

routes.inactive.route = “/school/inactive/:school”
routes.inactive.defaults.controller = index
routes.inactive.defaults.action = inactive
routes.inactive.defaults.module = school

Where routes is the key – inactive is the route name. The rest is fairly obvious if you’ve read the Router docs.

Now in your application Bootstrap create a method to initiate this route file and inject it into the controller.

	protected function _initRoutes()
	{
		// get your controller
                $ctrl = Zend_Controller_Front::getInstance();

		$router = $ctrl->getRouter();
		// initialise a new zend config object		
		$routeConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');	

                // via the add config method your routes are added automagically
		$router->addConfig($routeConfig, 'routes');

		return $router;
	}

There you have it. Routes stored in a much more managable fashion.

To use the routes in your view scripts using the url helper you can now do the following.

<?php echo $this->url(array(), 'inactive', $requestParamsArray); ?>

Zend Framework Cron Job Script

Another little titbit of code to run cronjobs from Zend Framework Applications.

I usually make this file inside /application//cronjobs/. It’s a copy of the Bootstrap, which I happen to think makes writing cron jobs hella easy!

// important.. make sure we're working out of here.
chdir(dirname(__FILE__));

define('BASE_PATH', realpath(dirname(__FILE__) . '/../../'));
define('APPLICATION_PATH', BASE_PATH . '/application');


// Include path
set_include_path(
    BASE_PATH . '/library'
    . PATH_SEPARATOR . get_include_path()
);

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV',
              (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
                                         : 'development'));

// Zend_Application
require_once 'Zend/Application.php';

$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

$application->bootstrap();


// cron job code goes here.

THis file can realisitically live anywhere within your application structure. I would recommend moving this into a separate file then in each cronjob add a call to require once to this file.


require_once "appLoader.php";

// now we can run external cron job code in Zf.