Flintstone 2.0 released

I am delighted to announce version 2.0 of Flintstone has now been released.

Changes include:

  • Major refactor, class names have changed and the whole codebase is much more extensible
  • Removed the static load and unload methods and the FlinstoneDB class
  • The replace method is no longer public
  • The getFile method has been removed
  • Default swap memory limit has been increased to 2MB
  • Ability to pass any instance for cache that implements Flintstone\Cache\CacheInterface

View on Github

Data transformer class

When building an API it is common for people to just grab stuff from the database and pass it to json_encode(). The problem with this approach is that it can quickly lead to inconsistent output - for example when a database table schema changes.

A data transformer acts as the middle-man between the data fetched and what is output to ensure consistency. Think of it as a view layer for your data. Below is a transformer class and example that you can extend to write your own transformers.

Read more »

The working directory in PHP

A colleague recently came to me with this issue that worked fine in their browser, but not on the command line:

Warning: include(../config.php): failed to open stream: No such file or directory

This warning appeared because of the current working directory.

When accessing this script via the browser, Apache found the correct path to the file and so '../' was relative to it's own directory.

However by running it from a command line...

php /var/www/public/index.php

The current working directory was actually wherever the user is in the file system of the server.

In which case they could do:

cd /var/www/public/
php index.php

Better yet, they could use the __DIR__ magic constant in PHP which returns the directory of the file itself.

include(__DIR__ . '/../config.php');

If you ever need to figure out the current working directory, you can use the nifty function getcwd().

Google Maps - dragging a marker to new position

Something that took me a while to figure out was how to drag a marker on a map to a new position and then retrieve it's lat/lng (version 3).

This assumes you have the map variable set as an instance of google.maps.Map and latlng which is the current position to show on the map as an instance of google.maps.LatLng.

var marker = new google.maps.Marker({
	map: map,
	draggable: true,
	position: latlng

google.maps.event.addListener(marker, 'dragend', function(){
	var position = marker.getPosition();
	map.setCenter(position); // set the map center to it's new position
	alert(position.lat() + ',' + position.lng()); // here are the new lat/lng strings

Simple PHP caching improved

There are many tools around that will help cache your web pages to increase speed and performance (most notably Memcached) however these usually require installation on your web server, thus rendering them useless for people who use shared hosting and don't have access to the server.

Using some simple PHP code we can quite effectively cache a webpage, useful for sites that are database driven and have a large amount of queries or high volumes of traffic.

First we need to setup the folder where the cached files are stored, making sure it has read/write permissions (CHMOD to 0777 - see your FTP client documentation on how to do this).

Next create a file called cache.php which contains the following code:

Read more »