PHP equivalent construction for Ruby `__END__` or Perl` __DATA__`

I am using PHP from the command line and I am trying to insert data into a source code file. I can do this in Ruby using the __END__ construct and in Perl using the __DATA__ construct, but I cannot find a way to do this in PHP. The goal is to be able to insert data into a PHP file, and then at runtime read the data into an array for processing. How can I do this in PHP? Including data in a separate file is not really a good option because of how the data and file execution are configured.

+6
source share
3 answers

You can use __halt_compiler() , the purpose of which, as the name implies, is to stop the compiler with the goal of embedding data in the rest of the file.

Basic example:

 <?php // Do something boring here $data = file_get_contents(__FILE__, FALSE, NULL, __COMPILER_HALT_OFFSET__); $obj = json_decode($data, TRUE); echo $obj['message']; __halt_compiler(); {"status":"example", "message":"hello, __halt_compiler!"} 

The output, as you would expect, is hello, __halt_compiler!

+7
source

Use the __halt_compiler() construct:

 <?php echo 'hello!'; __halt_compiler(); echo 'bar''; this will not cause a parse error 
+4
source

you can use __ halt_compiler () and insert data after this line of code. The documentation has a great example.

+3
source

Source: https://habr.com/ru/post/895184/


All Articles