Skip to content

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

License

Notifications You must be signed in to change notification settings

phppackage/magic-class

Repository files navigation

Magic Class

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages