Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate php source code based on php array

I've got a non-modifiable function which takes several seconds to finish. The function returns an array of objects. The result only changes about once per day.

To speed things up I wanted to cache the result using APC but the hosting provider(shared hosting environment) does not offer any memory caching solutions (APC, memcache, ...).

The only solution I found was using serialize() to store the data into a file and then deserializing the data back again.

What about generating php source code out of the array? Later I could simple call

require data.php

to get the data into a predefined variable.

Thanks!

UPDATE: Storing the resulting .html is no option because the output is user-dependant.

like image 496
Markus Hi Avatar asked Dec 28 '22 23:12

Markus Hi


1 Answers

Do you mean something like this?

// File: data.php
<?php
return array(
  32,
  42
);


// Another file
$result = include 'data.php';
var_dump($result);

This is already possible. To update your file, you can use something like this

file_put_contents('data.php', '<?php return ' . var_export($array, true) . ';');

Update: However, there is also nothing wrong with serialize()/unserialize() and storing the serialized array into a file.

like image 183
KingCrunch Avatar answered Dec 31 '22 12:12

KingCrunch