|
|
PHP var_export
|
|
|
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
I'm trying to save some data to disk as a cache so it doesn't get calculated on every page view. I used to export flat HTML, but I'd like to output just the data structures instead. I used var_export to save it, but when I read the data in I get a parse error (unexpected T_CLASS).
What I'm outputting is an object of a class "Table" that I created. One of the variables of a Table is $rows, which is an array that holds objects of class "Row." The parse error I mentioned above specifies the first line in the file where a Row object is defined. It looks like this:
Code:
class Table {
public $headers = blah;
public $rows =
array (
0 =>
class Row {
public $cells = blah;
}
)
}
Is this not legal? I thought the whole point of var_export was that the output was valid PHP, but if nesting objects like this is illegal I wish it would have told me during the export, not upon parsing the output.
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
The way you've written the code is certainly illegal, because you can't have a class definition as an array element.
Perhaps something more along the lines of...
[php]
class table
{
public $header = 'Blah';
public $rows;
function __construct()
{
$this->rows = array(
array(0=>'Data1',1=>'Data2',etc...),
array(0=>'Data1',1=>'Data2',etc...),
);
}
}
[/php]
is what you're after?
Why not save it as XML instead - the simpleXML object is very inexpensive processor/timewise and you have your data in a more accessible format. Just a thought!
Good luck
|
Computer thez nohhh...
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
The problem is that it's not me that made that code. PHP is making that code with the var_export function. It's too bad var_export is allowed to do illegal things like that. I guess I'll have to try something else. Thanks for the XML suggestion.
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
|
Computer thez nohhh...
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
I had looked at the var_export manual a couple times, but only noticed the warning about objects-inside-objects just now. Thanks.
I was looking for serialize but I couldn't remember what it was called. I'll see what I can do with that. Thanks again.
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Oct 2003
Status:
Offline
|
|
yeah go with serialize... I've never really used var_export myself, but I have never had any trouble with serializing / unserializing any sort of PHP data types (objects, arrays, strings... whatever... it will handle them all)
|
The only thing necessary for evil to flourish is for good men to do nothing
- Edmund Burke
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jan 2002
Status:
Offline
|
|
Serializing is also the proper term, and was adapted from Java et al for the conversion of objects to XML data.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|