Available from: PHP 4.2.0
This function returns a string that represents the inserted variable as if it were written in a PHP script. The output can be used directly for a PHP parser.
| Parameter | Data type | Default value | Note |
|---|---|---|---|
$expression |
mixed |
not | Variable with the data we want to export. |
$return |
bool |
null | If true, the function returns the value of the parameter instead of using it directly. |
mixed
This function returns the representation of the inserted variable if the second parameter was true.
If the second parameter was false, the function always returns null.
Let's have an input:
$a = [1, 2, ['a', 'b', 'c']];var_export($a);
Returns:
array (
0 => 1,
1 => 2,
2 =>
array (
0 => 'a',
1 => 'b',
2 => 'c',
),
)
Often you need to dump (export) objects with a very useful structure, or even generate an entire PHP class according to a specification.
The PhpGenerator package, which is directly part of the Nette framework, is a good way to do this.
It is easy to use:
$class = new Nette\PhpGenerator\ClassType('Demo');$class->setFinal()->setExtends('ParentClass')->addImplement('Countable')->addTrait('Nette\SmartObject')->addComment("Class description.\nSecond row\n")->addComment('@property-read Nette\Forms\Form $form');// you can generate the code simply by rewriting it to a string or using echo:echo $class;
Generates:
/*** Class description* Second line** @property-read Nette\Forms\Form $form*/final class Demo extends ParentClass implements Countable{use Nette\SmartObject;}
Dumping constants and arrays in variables:
$class->addConstant('ID', 123);$class->addProperty('items', [1, 2, 3])->setVisibility('private')->setStatic()->addComment('@var int[]');
Generates:
const ID = 123;
/** @var int[] */
private static $items = [1, 2, 3];
See official documentation for more examples.
[Official var-export documentation](- Official var_export manual)
Jan Barášek Více o autorovi
Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.
Rád vám pomůžu:
Články a novinky nejen ze světa PHP a programování. Nenechte si ujít jediný článek.
Články píše Jan Barášek © 2009-2025 | Kontakt | Mapa webu
Status | Aktualizováno: ... | en