PHP Classes
elePHPant
Icontem

MASNathan Super Object: Set and get variables, call objects in many ways

Recommend this page to a friend!
  Info   View files Documentation   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2015-06-17 (3 years ago) RSS 2.0 feedNot enough user ratingsTotal: 213 All time: 8,077 This week: 366Up
Version License PHP version Categories
masnathan-object 1.0.0MIT/X Consortium ...5.4PHP 5, Data types
Description Author

This class can set and get variables, call objects in many ways.

It provides multiple ways to set and get variables, either assigning many variables at once with an array of variable values to the constructor, or setting or getting variables using the variable names directly or setter and getter functions.

The class can also check variable values with "is a" type functions.

Innovation Award
PHP Programming Innovation award nominee
July 2015
Number 2


Prize: PhpStorm IDE personal permanent license
Sometimes you need to setup a structure of objects with an hierarchy defined from nested arrays, for instance parsed XML documents or other format.

This class allows you to create an hierarchy of objects from a single array of nested associative arrays.

Each nested array and converted to a new object that can be accessed with automatic getter and setter functions, as well to check the existence of variables or even remove them if they exist.

Manuel Lemos
  Performance   Level  
Name: Andre Roque Filipe is available for providing paid consulting. Contact Andre Roque Filipe .
Classes: 9 packages by
Country: Portugal Portugal
Age: 26
All time rank: 124616 in Portugal Portugal
Week rank: 919 Up14 in Portugal Portugal Up
Innovation award
Innovation award
Nominee: 2x

Details

Object

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads Support via Gittip

Super Object that can handle everything you throw at him...

Install

Via Composer

$ composer require masnathan/object

Usage

use MASNathan\Object;

$object = new Object();
$object->setMode('live');
$object->set('mode', 'live');
$object->mode = 'live';
$object['mode'] = 'live';

echo $object->getAppMode() // 'live'
echo $object->get('app_mode') // 'live'
echo $object->app_mode // 'live'
echo $object['mode'] // 'live'

So... let's suppose you have an array like this:

$myBigDataArray = array(
	'details' => array(
		'first_name' => 'André',
		'last_name' => 'Filipe',
		'email' => 'andre.r.flip@gmail.com',
		'social' => array(
			'github' => 'https://github.com/MASNathan',
			'twitter' => 'https://twitter.com/masnathan'
		)
	),
	'account_info' => array(
		'admin' => true,
		'last_login' => 2015-06-13 13:37:00
	)
	'cart_items' => array(
		array('id' => 1337),
		// (...)
	)
);

Using the `Object` class you can acces it's information like this:

$object = new Object($myBigDataArray);

echo $object->getDetails()->getFirstName(); // 'André'
$object->getDetails()->isLastName('Roque'); // false
echo $object->getDetails()->getSocial()->getGithub(); // 'https://github.com/MASNathan'
echo $object->getDetails()->getSocial()->getFacebook(); // ''
$object->getAccountInfo()->isAdmin(); // true
$object->getAccountInfo()->unsetLastLogin(); // unsets $myBigDataArray['account_info']['last_login']

foreach ($object->getCartItems() as $item) {
	echo $item->getId(); // 1337
}

You can also retrive the contents of the Object as an `array` or a `StdClass`:

$object->toArray(); // array( ... )
$object->toObject(); // StdClass( ... )

And even serialize/ deserialize the object

unserialize(serialize($object));
// or as json
json_decode(json_encode($object));

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email andre.r.flip@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file Object.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file ObjectTest.php Test Unit test script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:213
This week:0
All time:8,077
This week:366Up