Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP var_export

PHP var_export
Thread Tools
wataru
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Jul 26, 2005, 03:38 AM
 
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.
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Jul 26, 2005, 03:51 AM
 
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...
     
wataru  (op)
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Jul 26, 2005, 03:55 AM
 
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.
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Jul 26, 2005, 06:56 AM
 
Originally Posted by wataru
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.
http://php.planetmirror.com/manual/e...var-export.php

Note: Variables of type resource and arrays or objects containing objects couldn't be exported by this function.
WHat you could try, though is:-

http://php.planetmirror.com/manual/e....serialize.php
Computer thez nohhh...
     
wataru  (op)
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Jul 26, 2005, 07:16 AM
 
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.
     
madmacgames
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Jul 26, 2005, 10:00 AM
 
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
     
Peder Rice
Mac Enthusiast
Join Date: Jan 2002
Status: Offline
Reply With Quote
Jul 26, 2005, 10:35 AM
 
Serializing is also the proper term, and was adapted from Java et al for the conversion of objects to XML data.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:14 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,