phppackage/magic-class

v1.0.2 2018-02-08 11:23 UTC

This package is not auto-updated.

Last update: 2024-12-17 05:17:34 UTC


README

Build Status StyleCI Scrutinizer Code Quality Code Coverage Packagist Version Packagist Downloads

A magical PHP class, which can be treated as an array or an object, which toString's to json.

Install

Require this package with composer using the following command:

$ composer require phppackage/magic-class

Usage example:

<?php
require 'vendor/autoload.php';
//
use PHPPackage\MagicClass;

// pre-initialize with constructor arguments
$magicClass = new MagicClass('some value');

// access as array or object
echo $magicClass[0]   // some value
echo $magicClass->{0} // some value

// an empty instance
$magicClass = new MagicClass();

// add values
$magicClass['foo'] = 'BarBaz';

// access as array or object
echo $magicClass['foo']; // BarBaz
echo $magicClass->foo;   // BarBaz

// variables can be invoked
$magicClass->object = new class {
    private $foo = 'BarBaz';

    public function getFoo()
    {
        return $this->foo;
    }
};
echo $magicClass('object')->getFoo(); // BarBaz

// toString dumps to json
$magicClass['foo'] = 'BarBaz';

echo strval($magicClass); // {"foo": "BarBaz"}

// count elements
echo count($magicClass) // 1

// var_dump/print_r clean output
print_r($magicClass);

PHPPackage\MagicClass Object
(
    [string] => BarBaz
)

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Credits

License

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