This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

A magical PHP class

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

This package is not auto-updated.

Last update: 2021-10-11 13:12:49 UTC


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.


Require this package with composer using the following command:

$ composer require phppackage/magic-class

Usage example:

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

PHPPackage\MagicClass Object
    [string] => BarBaz


$ composer test


Please see CONTRIBUTING for details.



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