PHPackages                             snscripts/result - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. snscripts/result

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

snscripts/result
================

Framework agnostic Result Object

1.0.3(9y ago)197.9k↓23.7%13MITPHPPHP &gt;=5.5.0

Since Oct 15Pushed 9y ago1 watchersCompare

[ Source](https://github.com/mikebarlow/result)[ Packagist](https://packagist.org/packages/snscripts/result)[ Docs](https://github.com/mikebarlow/Result)[ RSS](/packages/snscripts-result/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (6)Used By (3)

Framework Agnostic Results Object
=================================

[](#framework-agnostic-results-object)

[![Author](https://camo.githubusercontent.com/9d903e7623152bba92e485dae02e0ffe50922554087701bab4ab685ea10150d2/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d406d696b656261726c6f772d7265642e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/mikebarlow)[![Source Code](https://camo.githubusercontent.com/889b58bb2e4647292337967abb3c01ac5d75857bd0aed9a822760321b33dc40b/687474703a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d6d696b656261726c6f772f726573756c742d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/mikebarlow/result)[![Latest Version](https://camo.githubusercontent.com/56ddb5c8c5a73dc8fe6ca8bbca65f53585698911a21f0157d85cd25b0f17d26c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6d696b656261726c6f772f726573756c742e7376673f7374796c653d666c61742d737175617265)](https://github.com/mikebarlow/result/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/mikebarlow/result/blob/master/LICENSE)[![Build Status](https://camo.githubusercontent.com/a05f20c8b1a46290b342b2024495367d0a13e521bac5912f9683fb922c514304/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d696b656261726c6f772f726573756c742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/mikebarlow/result)

Introduction
------------

[](#introduction)

This Result Object is a PSR-2 Compliant Framework Agnostic helper designed for returning data in an object orientated way. It allows you do easily define the success or failure of the recent action as well as set easily checkable status strings, fuller messages, error strings and any extra data.

Requirements
------------

[](#requirements)

### Composer

[](#composer)

Snscripts\\Result requires the following:

- "php": "&gt;=5.5.0"

And the following if you wish to run in dev mode and run tests.

- "phpunit/phpunit": "~4.0"
- "squizlabs/php\_codesniffer": "~2.0"

Installation
------------

[](#installation)

### Composer

[](#composer-1)

Simplest installation is via composer.

```
composer require snscripts/result 1.*

```

or adding to your projects `composer.json` file.

```
{
    "require": {
        "snscripts/result": "1.*"
    }
}

```

Usage
-----

[](#usage)

In it's simplest form the package can be used in the following ways

```
// A successful result
$Result = \Snscripts\Result\Result::success();

// A failure result
$Result = \Snscripts\Result\Result::fail();

```

You would then need to return `$Result` from your function or library, within the code that called your library you can then check whether the result was a success or a fail depending on your needs

```
$Result = $MyLibrary->someAction(); // this returns Result::success();

// check for a success
var_dump($Result->isSuccess());

// check for a fail
var_dump($Result->isFail());

```

### Status Codes

[](#status-codes)

Sometimes you may wish to define a status code to go with the result, this can further describe the result. This can also be useful in multi-lingual situations where your code requires a fixed string / code to define exactly what happened.

You can set a code using `$Result->setCode('myCode');` and retrieve the code from the result using `$Result->getCode();`.

As default the object comes with some defined constants for common results.

```
CREATED    = 'created',
UPDATED    = 'updated',
SAVED      = 'saved',
DELETED    = 'deleted',
VALIDATION = 'validation',
AUTH       = 'authorised',
NOT_AUTH   = 'not_authorised',
FOUND      = 'found',
NOT_FOUND  = 'not_found',
ERROR      = 'error',
FAILED     = 'failed',
PROCESSING = 'processing'

```

These are object constants so would be accessed in the following way.

```
use Snscripts\Result\Result;

var_dump(Result::CREATED);
var_dump(Result::AUTH);
var_dump(Result::NOT_FOUND);

```

This means you can use them in the following way in combination with `setCode` method above.

```
$Result->setCode(Result::ERROR);

echo $Result->getCode(); // 'error'

```

The error code can also be passed as the first parameter of the static `success` and `fail` methods.

```
Result::success(Result::CREATED);
Result::fail(Result::VALIDATION);

```

Message
-------

[](#message)

Along with the status code you can also define a message to pass along. This could be the actual message that is displayed to the user, a language string for translation, etc...

This is set and retrieved in the following way.

```
$Result->setMessage('Your item was added successfully');
$Result->getMessage();

```

For convience, a message can also be passed as the second parameter of the static `success` and `fail` methods.

```
Result::success(
	Result::CREATED,
	'Your item was created successfully'
);
Result::fail(
	Result::VALIDATION,
	'There was a validation error, please try again'
);

```

Errors
------

[](#errors)

If the result is a fail result, you may need to pass an array of errors. This will generally be from your systems validation methods.

You can set a full array of errors in one go by using the `setErrors` command.

```
$Result->setErrors([
	'field' => [
		'is required,
		'must be unique'
	],
	'another_field' => 'is required'
]);

```

You can then retrieve the error list very simply by calling

```
var_dump($Result->getErrors());

```

In some cases you may be looping through your data and need to set error messages one at a time.

You can do this by using `setError`

```
$Result->setError('Field is required');
$Result->setError(['field' => 'is required']);

```

The `setError` method has been written so any strings set will be pushed onto the array as normal.

```
$Result->setError('Field is required');
$Result->setError('Another error occurred');

// var_dump($Result->getErrors()); Will result in
[
	'Field is required',
	'Another error occurred'
]

```

In the event of an array being passed, the method will try and merge the array in. This helps prevent an extra array level being created and also helps merge data if multiple errors occurred on the same key.

```
$Result->setError('Field is required');
$Result->setError(['field' => 'is required']);
// var_dump($Result->getErrors()); Will result in
[
	'Field is required',
	'field' => 'is required'
]

```

For convience, you can pass an array of errors into the third parameter of the `success` and `fail` static methods, this will perform the equivilant of the `setErrors` command and will set the complete array of errors.

```
Result::fail(
	Result::VALIDATION,
	'There was a validation error, please try again',
	[
		'field1' => 'is required',
		'field2' => [
			'is required',
			'should be unique'
		]
	]
);

```

Extras
------

[](#extras)

With any result you may wish to passed some extra data back with the result, this could be a copy of the data just manipulated or a copy of the form array that just failed validation, with Result you can pass this simply with the following commands.

You can set a full array of extra data in one go by using the `setExtras` command.

```
$Result->setExtras([
	'foo' => 'bar',
	'bar' => 'foo',
	[
		'fizzbuzz',
		'buzzfizz'
	]
]);

```

You can then retrieve the extra data list very simply by calling

```
var_dump($Result->getExtras());

```

In some cases you may be looping through your data and need to set data one piece at a time.

You can do this by using `setExtra`

```
$Result->setExtra('foobar');
$Result->setExtra(['foo' => 'bar']);

```

The `setExtra` method has been written so any strings set will be pushed onto the array as normal.

```
$Result->setExtra('foobar');
$Result->setExtra('barfoo');

// var_dump($Result->getExtras()); Will result in
[
	'foobar',
	'barfoo'
]

```

In the event of an array being passed, the method will try and merge the array in. This helps prevent an extra array level being created and also helps merge data if multiple items occurred on the same key.

```
$Result->setExtra('foobar');
$Result->setExtra(['foo' => 'bar']);
// var_dump($Result->getExtras()); Will result in
[
	'Foobar',
	'foo' => 'bar'
]

```

For convience, you can pass an array of extra data into the fourth parameter of the `success` and `fail` static methods, this will perform the equivilant of the `setExtras` command and will set the complete array of data.

```
Result::success(
	Result::CREATED,
	'Your item was successfully created',
	[],
	[
		'id' => 1,
		'name' => 'Foo Bar',
		'address' => 'fizzbuzz street'
	]
);

```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/mikebarlow/result/blob/master/CONTRIBUTING.md) for details.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/mikebarlow/result/blob/master/LICENSE) for more information.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~18 days

Total

5

Last Release

3429d ago

Major Versions

0.1.0 → 1.0.02016-10-16

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/43972359?v=4)[snsmurf](/maintainers/snsmurf)[@snsmurf](https://github.com/snsmurf)

---

Top Contributors

[![mikebarlow](https://avatars.githubusercontent.com/u/293049?v=4)](https://github.com/mikebarlow "mikebarlow (9 commits)")

---

Tags

resultsframework agnosticReturn Statuses

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/snscripts-result/health.svg)

```
[![Health](https://phpackages.com/badges/snscripts-result/health.svg)](https://phpackages.com/packages/snscripts-result)
```

###  Alternatives

[php-flasher/flasher

The foundational PHP library for PHPFlasher, enabling the creation of framework-agnostic flash notifications. Ideal for building custom integrations or for use in PHP projects.

634.6M32](/packages/php-flasher-flasher)[ropendev/datatablesphp

PHP DataTables wrapper class for DataTables.js (Html and/or Javascript generation, Server-Side SQL) .

286.7k](/packages/ropendev-datatablesphp)[godbout/alfred-workflow-scriptfilter

Generate Alfred 3 or 4 Workflow Results in PHP with a laugh.

173.7k1](/packages/godbout-alfred-workflow-scriptfilter)[rumenx/php-seo

AI-powered, framework-agnostic PHP package for automated SEO optimization. Intelligently generates meta tags, titles, descriptions, and alt texts using configurable AI providers or manual patterns.

102.0k](/packages/rumenx-php-seo)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
