File System Functionality

Solar comes with two static utility classes for file and directory functions. We won't go over all their methods in detail, but they include:

Solar_Dir::exists()
See if a directory exists in the include-path.
Solar_Dir::fix()
"Fix" a directory name for the OS.
Solar_Dir::name()
A replacement for php::dirname() that lets you specify how many levels "upward" you want to traverse.
Solar_Dir::tmp()
Returns the temporary directory name for the OS.
Solar_File::exists()
See if a file exists in the include-path.
Solar_File::load()
Include a file in a limited scope.
Solar_File::tmp()
Get a temporary file name.

Solar_File::load()

Use the Solar_File::load() method to include a file inside its own limited scope. This allows you to include files but not pollute the current scope with the variables from the included file.

For example, say you have this "main" script:

<?php
$var = 'foo';
include 'script.php';
echo $var;
?>

If 'script.php' sets the value of $var for itself, that value will override the value from the main script, which can lead to unexpected behavior. If you want to make sure that 'script.php' executes in its own separate scope, use Solar_File::load() instead of include() or require():

<?php
$var = 'foo';
Solar_File::load('script.php');
echo $var;
?>

Now $var will remain the same before and after the inclusion of script.php (unless script.php calls global to make $var global).

As with include() and require, you can accept return values from the file you run:

<?php
$result = Solar_File::load('script.php');
?>

If the last line of script.php is a return value, $result will reflect that value.

 
manual/file_system/overview.txt · Last modified: 2008/08/25 08:48 (external edit)