PHPackages                             wid/native-object - 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. wid/native-object

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

wid/native-object
=================

Keep your data in native php types!

06PHP

Since Jan 17Pushed 12y ago1 watchersCompare

[ Source](https://github.com/daveWid/NativeObject)[ Packagist](https://packagist.org/packages/wid/native-object)[ RSS](/packages/wid-native-object/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

NativeObject
============

[](#nativeobject)

PHP likes to send data back as strings when pulling from datasources. This library will help you convert everything into a native php format.

Example
-------

[](#example)

The easiest way to learn about this library is to see an example. We'll pretend that we are converting a row from a database.

The property map is an array of *'property name' =&gt; 'mapping name'* pairs. See the [Casting](#casting) section for more information on what is available.

```
class UserObject extends Wid\Data\NativeObject
{
	protected function init()
	{
		$this->setPropertyMap(array(
			'id' => 'Integer',
			'active' => 'Boolean',
			'joined_at' => 'DateTime',
			'roles' => function($arg){
				return explode(',', $arg);
			}
		));

		parent::init();
	}
}

$databaseRow = array(
 	'id' => '1',
	'username' => 'dwidmer',
 	'active' => '0',
 	'joined_at' => '2014-01-15 12:00:00',
	'roles' => 'super,admin,user'
);

$user = new UserObject($databaseRow);
$user->toArray();

/* //// Output //// */

// 'id' => (int) 1
// 'username' => (string) 'dwidmer'
// 'active' => (boolean) false,
// 'joined_at' => \DateTime
// 'roles' => array('super', 'admin', 'user')
```

It's really that easy!

Casting
-------

[](#casting)

Below is a list of casting types available in the library

Mapping NameTransforms toBooleanbooleanIntegerintFloatfloatDateTime\\DateTimeDate\\DateTimeTime\\DateTimeTimestamp\\DateTimeYou can also pass in anything that can be called by `call_user_func` as the second argument in the propery map (see `roles` in the example) and the data can be casted that way. In our example it is turning a string into an array, exploding on the commas.

Property Access
---------------

[](#property-access)

So how do I get at the data?

Well each of the following lines will work.

```
$id = $user['id']; // Array Access
$id = $user->id; // Object Notation
$id = $user->get('id'); // Getter
```

`get()` has the added bonus of being able to add a default value as the second parameter. This is `null` if not explicitly set.

And setting?

You can set with Array Access, Object Notation or a Setter, just like when you are getting data out. All of the methods below will work.

```
$user['active'] = true;
$user->active = true;
$user->set('active', true);
```

Why?
----

[](#why)

Getting our data into native php has a lot of advantages, but the one I had in mind when develping this library is for API development. When we send our data it should be in native data types (i.e. booleans shouldn't come back as '0'). *P.S. - you can call `toJSON()` on any NativeObject and get the json representation of it.*

Development
-----------

[](#development)

See some things that can be improved or want to help add some features?

- Clone this repo
- Run `composer install`
- Code away (with phpunit tests to back up your changes!)
- Send me a pull request!

---

Developed by [Dave Widmer](http://www.davewidmer.net)

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/46fa0decb95ad8d80b65e1fbed54f3fa9ee6350f1c8a0f83335221a201effb59?d=identicon)[davewid](/maintainers/davewid)

### Embed Badge

![Health badge](/badges/wid-native-object/health.svg)

```
[![Health](https://phpackages.com/badges/wid-native-object/health.svg)](https://phpackages.com/packages/wid-native-object)
```

###  Alternatives

[awcodes/filament-badgeable-column

Filament Tables column to append and prepend badges.

142419.3k3](/packages/awcodes-filament-badgeable-column)[metaregistrar/php-dns-client

A php-only, object-oriented client that is able to read and process DNS records

297.6k](/packages/metaregistrar-php-dns-client)

PHPackages © 2026

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