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
<?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
)
$ composer test
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.