Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Library for diffing, patching and representing differences between complex objects
Branch: master

Merge pull request #45 from wmde/changeIrc

Move Travis IRC notifications to #wikidata-feed
latest commit 1f204fc98c
@addshore addshore authored


Build Status Code Coverage Scrutinizer Quality Score Dependency Status Latest Stable Version Download count

Diff is a small standalone PHP library for representing differences between data structures, computing such differences, and applying them as patches. It is extremely well tested and allows users to define their own comparison strategies.

Diff does not provide any support for computing or representing the differences between unstructured data, ie text.

Recent changes can be found in the release notes.


  • PHP 5.3 or later (tested with PHP 5.3 up to PHP 5.6 and hhvm)


You can use Composer to download and install this package as well as its dependencies. Alternatively you can simply clone the git repository and take care of loading yourself.


To add this package as a local, per-project dependency to your project, simply add a dependency on diff/diff to your project's composer.json file. Here is a minimal example of a composer.json file that just defines a dependency on Diff 2.x:

    "require": {
        "diff/diff": "2.*"


Get the Diff code, either via git, or some other means. Also get all dependencies. You can find a list of the dependencies in the "require" section of the composer.json file. Load all dependencies and the load the Diff library by including its entry point: Diff.php.

High level structure

The Diff library can be subdivided into several components. The main components are:

  • DiffOp Value objects that represent add, change, remove and composite operations.
  • Differ Service objects to create a diff between two sets of data.
  • Patcher Service objects to apply a diff as patch to a set of data.

There are two support components, which are nevertheless package public:

  • Comparer Service objects for determining if two values are equal.
  • ArrayComparer Service objects for computing the difference between to arrays.


Representing diffs

A diff consists out of diff operations. These can be atomic operations such as add, change and remove. These can also be diffs themselves, when dealing with nested structures. Hence the composite pattern is used.

Diff operations implement the DiffOp interface.

The available operations are:

  • DiffOpAdd - addition of a value (newValue)
  • DiffOpChange - modification of a value (oldValue, newValue)
  • DiffOpRemove - removal of a value (oldValue)
  • Diff - a collection of diff operations

These can all be found in src/DiffOp.

The Diff class can be set to be either associative or non-associative. In case of the later, only DiffOpAdd and DiffOpRemove are allowed in it.

Diffing data

To compute the difference between two data structures, an instance of Differ is used. The Differ interface has a single method.

 * Takes two arrays, computes the diff, and returns this diff as an array of DiffOp.
 * @since 0.4
 * @param array $oldValues The first array
 * @param array $newValues The second array
 * @throws Exception
 * @return DiffOp[]
public function doDiff( array $oldValues, array $newValues );

Implementations provided by Diff:

  • ListDiffer: Differ that only looks at the values of the arrays (and thus ignores key differences).
  • MapDiffer: Differ that does an associative diff between two arrays, with the option to do this recursively.
  • CallbackListDiffer: Since 0.5. Differ that only looks at the values of the arrays and compares them with a callback.
  • OrderedListDiffer: Since 0.9. Differ that looks at the order of the values and the values of the arrays.

All differ functionality can be found in src/Differ.

Applying patches

To apply a diff as a patch onto a data structure, an instance of Patcher is used. The Patcher interface has a single method.

 * Applies the applicable operations from the provided diff to
 * the provided base value.
 * @since 0.4
 * @param array $base
 * @param Diff $diffOps
 * @return array
public function patch( array $base, Diff $diffOps );

Implementations provided by Diff:

  • ListPatcher: Applies non-associative diffs to a base. With default options does the reverse of ListDiffer
  • MapPatcher: Applies diff to a base, recursively if needed. With default options does the reverse of MapDiffer

All classes part of the patcher component can be found in src/Patcher


Added in 0.6

The ValueComparer interface contains one method:

 * @since 0.6
 * @param mixed $firstValue
 * @param mixed $secondValue
 * @return bool
public function valuesAreEqual( $firstValue, $secondValue );

Implementations provided by Diff:

  • StrictComparer: Value comparer that uses PHPs native strict equality check (ie ===).
  • CallbackComparer: Adapter around a comparison callback that implements the ValueComparer interface.
  • ComparableComparer: Since 0.9. Value comparer for objects that provide an equals method taking a single argument.

All classes part of the ValueComparer component can be found in src/Comparer


Added in 0.8

The ArrayComposer interface contains one method:

 * Returns an array containing all the entries from arrayOne that are not present
 * in arrayTwo.
 * Implementations are allowed to hold quantity into account or to disregard it.
 * @since 0.8
 * @param array $firstArray
 * @param array $secondArray
 * @return array
public function diffArrays( array $firstArray, array $secondArray );

Implementations provided by Diff:

  • NativeArrayComparer: Adapter for PHPs native array_diff method.
  • StrategicArrayComparer: Computes the difference between two arrays by comparing elements with a ValueComparer.
  • StrictArrayComparer: Does strict comparison of values and holds quantity into account.
  • OrderedArrayComparer: Since 0.9. Computes the difference between two ordered arrays by comparing elements with a ValueComparer.

All classes part of the ArrayComparer component can be found in src/ArrayComparer


Manually constructing a diff

$diff = new Diff( array(
    'email' => new DiffOpAdd( '' ),
    'awesome' => new DiffOpChange( 42, 9001 ),
) );

Computing a diff

$oldVersion = array(
    'awesome' => 42,

$newVersion = array(
    'email' => '',
    'awesome' => 9001,

$differ = new MapDiffer();
$diff = $differ->diff( $oldVersion, $newVersion );

Applying a diff as patch

$oldVersion = array(
    /* ... */

$diff = new Diff( /* ... */ );

$patcher = new MapPatcher();
$newVersion = $patcher->patch( $oldVersion, $diff );


Something went wrong with that request. Please try again.