PHP code example of francislavoie / varexporter
1. Go to this page and download the library: Download francislavoie/varexporter library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
francislavoie / varexporter example snippets
use Brick \VarExporter \VarExporter ;
echo VarExporter::export([1 , 2 , ['foo' => 'bar' , 'baz' => []]]);
[
1 ,
2 ,
[
'foo' => 'bar' ,
'baz' => []
]
]
array (
0 => 1 ,
1 => 2 ,
2 =>
array (
'foo' => 'bar' ,
'baz' =>
array (
),
),
)
var_export(json_decode('
{
"foo": "bar",
"baz": {
"hello": "world"
}
}
' ));
stdClass::__set_state(array (
'foo' => 'bar' ,
'baz' =>
stdClass::__set_state(array (
'hello' => 'world' ,
)),
))
echo VarExporter::export(json_decode('
{
"foo": "bar",
"baz": {
"hello": "world"
}
}
' ));
(object) [
'foo' => 'bar' ,
'baz' => (object) [
'hello' => 'world'
]
]
class Foo
{
public $a;
public $b;
public $c;
public static function __set_state (array $array) : self
{
$object = new self ;
$object->a = $array['a' ];
$object->b = $array['b' ];
$object->c = $array['c' ];
return $object;
}
}
public static function __set_state (array $array) : self
{
$object = new self ;
foreach ($array as $key => $value) {
$object->{$key} = $value;
}
return $object;
}
\My\CustomClass::__set_state([
'foo' => 'Hello' ,
'bar' => 'World'
])
(static function () {
$class = new \ReflectionClass(\My\CustomClass::class);
$object = $class->newInstanceWithoutConstructor();
$object->__unserialize([
'foo' => 'Test' ,
'bar' => 1234
]);
return $object;
})()
(static function () {
$object = new \My\CustomClass;
$object->publicProp = 'Foo' ;
$object->dynamicProp = 'Bar' ;
return $object;
})()
(static function () {
$class = new \ReflectionClass(\My\CustomClass::class);
$object = $class->newInstanceWithoutConstructor();
...
})()
(static function () {
$class = new \ReflectionClass(\My\CustomClass::class);
$object = $class->newInstanceWithoutConstructor();
$object->publicProp = 'Foo' ;
$object->dynamicProp = 'Bar' ;
(function () {
$this ->protectedProp = 'contents' ;
$this ->privateProp = 'contents' ;
})->bindTo($object, \My\CustomClass::class)();
(function () {
$this ->privatePropInParent = 'contents' ;
})->bindTo($object, \My\ParentClass::class)();
return $object;
})()
echo VarExporter::export([
'callback' => function () {
return 'Hello, world!' ;
}
]);
[
'callback' => function () {
return 'Hello, world!' ;
}
]
namespace My \App ;
use My \App \Model \Entity ;
use function My \App \Functions \imported_function ;
use const My \App \Constants \IMPORTED_CONSTANT ;
use Brick \VarExporter \VarExporter ;
echo VarExporter::export(function (Service $service) : Entity {
strlen(NON_NAMESPACED_CONSTANT);
imported_function(IMPORTED_CONSTANT);
\My\App\Functions\explicitly_namespaced_function(\My\App\Constants\EXPLICITLY_NAMESPACED_CONSTANT);
return new Entity();
});
function (\My\App\Service $service) : \My \App \Model \Entity {
strlen(NON_NAMESPACED_CONSTANT);
\My\App\Functions\imported_function(\My\App\Constants\IMPORTED_CONSTANT);
\My\App\Functions\explicitly_namespaced_function(\My\App\Constants\EXPLICITLY_NAMESPACED_CONSTANT);
return new \My\App\Model\Entity();
}
VarExporter::export($var, VarExporter::ADD_RETURN | VarExporter::ADD_TYPE_HINTS);
return (...);
$object = $class->newInstanceWithoutConstructor();
(function () {
$this ->privateProp = ...;
})->bindTo($object, \My\CustomClass::class)();
VarExporter::export([
'one' => ['hello' , 'world' , 123 , true , false , null , 7.5 ],
'two' => ['hello' , 'world' , ['one' , 'two' , 'three' ]]
], VarExporter::INLINE_NUMERIC_SCALAR_ARRAY);
[
'one' => ['hello' , 'world' , 123 , true , false , null , 7.5 ],
'two' => [
'hello' ,
'world' ,
['one' , 'two' , 'three' ]
]
]
use Brick \VarExporter \VarExporter ;
use Brick \VarExporter \ExportException ;
try {
VarExporter::export(fopen('php://memory' , 'r' ));
} catch (ExportException $e) {
}