javalike-import-functie
Gesponsorde koppelingen
PHP script bestanden
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* Java-like import mechanism to load all files in a directory.
*
* @param string $package
* @return boolean
*/
function import( $package )
{
// Check for dots in the package-string.
if( !strpos( $package, '.' ) )
{
throw new Exception( 'I need some dots.' );
}
// Replace the dots with slashes.
$packageLocation = str_replace( '.', DIRECTORY_SEPARATOR, $package ); // 'test/*'
// Is this just one file, or a whole directory?
if( !substr( $packageLocation, -1 ) == '*' )
{
// Load the file.
require_once $packageLocation;
// Return boolean true.
return true;
}
// Real directory, without the last *.
$realLocation = substr( $packageLocation, 0, -1 );
// Is no directory?
if( !is_dir( $realLocation ) )
{
throw new Exception( 'Package string contains invalid directory.' );
}
// Perform a search for all files in the directory.
foreach( glob( $packageLocation . '.*' ) as $uniqueFile ) // add ".*" to the string so that only files will be found.
{
// Load the file.
require_once $uniqueFile;
}
// Return boolean true.
return true;
}
import( 'test.*' );
[/code]
/**
* Java-like import mechanism to load all files in a directory.
*
* @param string $package
* @return boolean
*/
function import( $package )
{
// Check for dots in the package-string.
if( !strpos( $package, '.' ) )
{
throw new Exception( 'I need some dots.' );
}
// Replace the dots with slashes.
$packageLocation = str_replace( '.', DIRECTORY_SEPARATOR, $package ); // 'test/*'
// Is this just one file, or a whole directory?
if( !substr( $packageLocation, -1 ) == '*' )
{
// Load the file.
require_once $packageLocation;
// Return boolean true.
return true;
}
// Real directory, without the last *.
$realLocation = substr( $packageLocation, 0, -1 );
// Is no directory?
if( !is_dir( $realLocation ) )
{
throw new Exception( 'Package string contains invalid directory.' );
}
// Perform a search for all files in the directory.
foreach( glob( $packageLocation . '.*' ) as $uniqueFile ) // add ".*" to the string so that only files will be found.
{
// Load the file.
require_once $uniqueFile;
}
// Return boolean true.
return true;
}
import( 'test.*' );
[/code]