WORLD'S WORST SOFTWARE
Software by jason, for jason.
Software by jason, for jason.
Home
About
Articles
Resume
Contact
Software
iPlaylist CopierJava Libraries
ItunesUtilitiesScripts
Bash Scripts
File Size File Size Test Free Space Line Counter List Copy MD5 Tool OS X Rotate Logs Rename It SnapshotPHP Scripts
Write Variable To File WWS MailerJavascript Scripts
WWS Background ScalerInteresting People
Admins
Phil GlocknerDesigners
Tonya Browning Catherine Chu Justin Cox Richard Georges Stephen Gray Jonathan Horak Justin Kiehl Andrea Spencer MeatheadDevelopers
Chris Austin Joost van Dongen Noah Figg Rick Hogge John Quarles Wayne Rittiman Joe Rivera Brent Schneeman David Scott Andy Skelton Pavan TumatiWrite Variable To File
This library helps php developers save the current state of a variable for future usage.
Developers can use the library to generate a valid php file with variable initialization code generated from the variable you provide, and then execute an include statement in another php file to include the instantiated variable.
Download
write_variable_to_file.zip [1.63 KB]
The Script
<?php
/***********************************************************************************************
*
* write variable to file library - (c) 2007 Jason Baker (http://www.worldsworstsoftware.com)
*
* This library helps php developers save the current state of a variable for future usage.
* developers can use the library to generate a valid php file with variable initialization code
* generated from the variable you provide, and then execute an include statement in another php
* file to include the instantiated variable.
*
*
***********************************************************************************************
*
* UPDATES:
*
* 2007/2/28
* - Initial version.
*
***********************************************************************************************/
/*
* get_variable_php - get php variable initialization code in string form, handles arrays and objects as well as primitives
*
* arguments:
* $variable - the variable to generate initialization code for
* $variable_name - the name of the variable that will be used in the initialization code
*
* example usage:
*
* $my_cat = new Cat();
* $my_cat->name = "samwise";
* $my_cat->weight = 123;
* $my_cat->favorite_toys = array();
* $my_cat->favorite_toys[0] = "stuffed mouse";
* $my_cat->favorite_toys[1] = "stuffed mouse #2";
* $my_cat->favorite_toys["absolute favorite"] = "stuffed mouse #3";
*
* echo get_variable_php($my_cat, "the_best_cat_ever");
*
* exampe output:
* $the_best_cat_ever = new Cat();
* $the_best_cat_ever->name = "samwise";
* $the_best_cat_ever->weight = 123;
* $the_best_cat_ever->favorite_toys = array();
* $the_best_cat_ever->favorite_toys[0] = "stuffed mouse";
* $the_best_cat_ever->favorite_toys[1] = "stuffed mouse #2";
* $the_best_cat_ever->favorite_toys["absolute favorite"] = "stuffed mouse #3";
*
* warning for classes without no-argument constructors:
*
* code generated by this function always instantiates objects with the no-argument constructor. php will throw
* a warning like the following if your class doesnt have this type of constructor:
*
* PHP Warning: Missing argument 2 for cat() in cat.php
*
* this warning can usually be ignored.
*/
function get_variable_php($variable, $variable_name)
{
$value = "";
//write out the variable at the beginning of the line if it's specified
if ($variable_name != "")
{
$value .= "$" . $variable_name . " = ";
}
if (is_array($variable))
{
$value .= "array();\n";
foreach($variable as $arraykey => $arrayvalue)
{
$value .= get_variable_php($arrayvalue, $variable_name . "[" . $arraykey . "]");
}
$variable_name = ""; //dont let the end statement be added later in the function
}
else if (is_string($variable))
{
$value .= "\"" . $variable . "\"";
}
else if (is_integer($variable) || is_double($variable) || is_float($variable) || is_long($variable))
{
$value .= $variable;
}
else if (is_bool($variable))
{
if ($variable)
{
$value .= "true";
}
else
{
$value .= "false";
}
}
else if (is_object($variable))
{
//instantiate the class with no-arg constructor
$value .= "new " . get_class($variable) . "();\n";
//iterate through and instantiate each property of the object
$vars = get_object_vars($variable);
foreach ($vars as $varkey => $varvalue)
{
$value .= get_variable_php($varvalue, $variable_name . "->" . $varkey);
}
$variable_name = ""; //dont let the end statement be added later in the function
}
else if (is_null($variable))
{
$value .= "NULL";
}
else
{
echo "get_variable_php does not know how to handle vars of type " . gettype($variable) . "\n";
exit;
}
if ($variable_name != "")
{
//if the variable name is specified, write out the semicolon and the end of the line
$value .= ";\n";
}
return $value;
}
/*
* write_variable_to_file - creates a php file with the initialization code for $var
*
* arguments:
* $var - the variable to generate php initialization code for
* $var_name - the name to call the variable in the generated code
* $filename - the filename to output the file to
*/
function write_variable_to_file($var, $var_name, $filename)
{
$output = "<?php\n\n" . get_variable_php($var, $var_name) . "\n?>\n";
if (!$handle = fopen($filename, 'w+'))
{
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $output) === FALSE)
{
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
}
?>
/***********************************************************************************************
*
* write variable to file library - (c) 2007 Jason Baker (http://www.worldsworstsoftware.com)
*
* This library helps php developers save the current state of a variable for future usage.
* developers can use the library to generate a valid php file with variable initialization code
* generated from the variable you provide, and then execute an include statement in another php
* file to include the instantiated variable.
*
*
***********************************************************************************************
*
* UPDATES:
*
* 2007/2/28
* - Initial version.
*
***********************************************************************************************/
/*
* get_variable_php - get php variable initialization code in string form, handles arrays and objects as well as primitives
*
* arguments:
* $variable - the variable to generate initialization code for
* $variable_name - the name of the variable that will be used in the initialization code
*
* example usage:
*
* $my_cat = new Cat();
* $my_cat->name = "samwise";
* $my_cat->weight = 123;
* $my_cat->favorite_toys = array();
* $my_cat->favorite_toys[0] = "stuffed mouse";
* $my_cat->favorite_toys[1] = "stuffed mouse #2";
* $my_cat->favorite_toys["absolute favorite"] = "stuffed mouse #3";
*
* echo get_variable_php($my_cat, "the_best_cat_ever");
*
* exampe output:
* $the_best_cat_ever = new Cat();
* $the_best_cat_ever->name = "samwise";
* $the_best_cat_ever->weight = 123;
* $the_best_cat_ever->favorite_toys = array();
* $the_best_cat_ever->favorite_toys[0] = "stuffed mouse";
* $the_best_cat_ever->favorite_toys[1] = "stuffed mouse #2";
* $the_best_cat_ever->favorite_toys["absolute favorite"] = "stuffed mouse #3";
*
* warning for classes without no-argument constructors:
*
* code generated by this function always instantiates objects with the no-argument constructor. php will throw
* a warning like the following if your class doesnt have this type of constructor:
*
* PHP Warning: Missing argument 2 for cat() in cat.php
*
* this warning can usually be ignored.
*/
function get_variable_php($variable, $variable_name)
{
$value = "";
//write out the variable at the beginning of the line if it's specified
if ($variable_name != "")
{
$value .= "$" . $variable_name . " = ";
}
if (is_array($variable))
{
$value .= "array();\n";
foreach($variable as $arraykey => $arrayvalue)
{
$value .= get_variable_php($arrayvalue, $variable_name . "[" . $arraykey . "]");
}
$variable_name = ""; //dont let the end statement be added later in the function
}
else if (is_string($variable))
{
$value .= "\"" . $variable . "\"";
}
else if (is_integer($variable) || is_double($variable) || is_float($variable) || is_long($variable))
{
$value .= $variable;
}
else if (is_bool($variable))
{
if ($variable)
{
$value .= "true";
}
else
{
$value .= "false";
}
}
else if (is_object($variable))
{
//instantiate the class with no-arg constructor
$value .= "new " . get_class($variable) . "();\n";
//iterate through and instantiate each property of the object
$vars = get_object_vars($variable);
foreach ($vars as $varkey => $varvalue)
{
$value .= get_variable_php($varvalue, $variable_name . "->" . $varkey);
}
$variable_name = ""; //dont let the end statement be added later in the function
}
else if (is_null($variable))
{
$value .= "NULL";
}
else
{
echo "get_variable_php does not know how to handle vars of type " . gettype($variable) . "\n";
exit;
}
if ($variable_name != "")
{
//if the variable name is specified, write out the semicolon and the end of the line
$value .= ";\n";
}
return $value;
}
/*
* write_variable_to_file - creates a php file with the initialization code for $var
*
* arguments:
* $var - the variable to generate php initialization code for
* $var_name - the name to call the variable in the generated code
* $filename - the filename to output the file to
*/
function write_variable_to_file($var, $var_name, $filename)
{
$output = "<?php\n\n" . get_variable_php($var, $var_name) . "\n?>\n";
if (!$handle = fopen($filename, 'w+'))
{
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $output) === FALSE)
{
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
}
?>
© 2006, 2007 Jason Baker