嗨,我真的很糟糕,也是PHP的新手。需要一些帮助。
我正在尝试在我的网站中定义一些常量:
代码1
define('SITE_ROOT',$_SERVER['DOCUMENT_ROOT'] . '/');
// Check if CORE_PATH is already defined else define here
defined('CORE_PATH') or define('CORE_ROOT', SITE_ROOT . '/CORE');
define('INC_PATH', IAO_ROOT . '/inc/');
define('LAYOUTS_PATH', IAO_ROOT . 'layouts/');
define('BLOCKS_PATH', SECTIONS_PATH . 'blocks/');
define('STATIC_PATH', BLOCKS_PATH . 'static/');
除了上面的例子,我还有另外10-15个常量来定义。我想知道在每行中定义每个常量是否正确,或者我可以执行以下操作:
代码2
define (
$constant = array (
'SITE_ROOT',
'CORE_PATH',
'INC_PATH' ,
'LAYOUTS_PATH',
'BLOCKS_PATH',
'STATIC_PATH'
),
$path = array(
$_SERVER['DOCUMENT_ROOT'] . '/',
SITE_ROOT . '/CORE',
CORE_PATH . '/inc',
CORE_PATH . '/layout',
CORE_PATH . '/blocks',
CORE_PATH . '/static'
)
);
define ( $constant, $path);
虽然代码1 在我的网站上正常运行,代码2 对我不起作用。 请告诉我什么是正确的方法。
更新 根据@LasVegasCoder更新了这个问题。不起作用。
<?php
//Create array of paths --example from your path ***use right paths***;
$path = array(
'SITE_ROOT . ' => $_SERVER['DOCUMENT_ROOT'],
'CORE_PATH' => SITE_ROOT . '/core',
'INCLUDE_PATH' => SITE_ROOT . '/inc',
'LAYOUT_PATH' => SITE_ROOT . '/layout',
'BLOCK_PATH' => SITE_ROOT . '/blocks',
'STATIC_PATH' => SITE_ROOT . '/static'
);
//usage:
createPath( $path );
//Testiing
echo SITE_ROOT; ?></br>
<?php echo CORE_PATH; ?></br>
<?php echo INCLUDE_PATH; ?></br>
<?php echo LAYOUT_PATH; ?></br>
<?php echo BLOCK_PATH; ?></br>
<?php echo STATIC_PATH; ?></br>
<?php
function createPath( $path )
{
if( empty( $path ) )
{
die("Array of path required!");
}
foreach( $path as $constant => $path )
{
if(!defined( strtoupper($constant) ) )
{
define( strtoupper($constant), $path . '/');
}
}
}
嗯,它仍然不起作用。有什么想法和解决方案吗?
答案 0 :(得分:1)
使用这个微小的功能,您可以创建路径为key =&gt;的数组; value,将其传递给函数以创建应用程序的路径。
创建路径数组
使用此问题中的示例 - 使用正确的路径
$path = array(
'SITE_ROOT' => $_SERVER['DOCUMENT_ROOT'],
'CORE_PATH' => '/core',
'INCLUDE_PATH' => '/inc',
'LAYOUT_PATH' => '/layout',
'BLOCK_PATH' => '/blocks',
'STATIC_PATH' => '/static'
);
用法使用函数创建路径:
createPath( $path );
测试路径
echo CORE_PATH;
<强>输出强>
/核心/
function createPath( $path )
{
if( empty( $path ) )
{
die("Array of path required!");
}
foreach( $path as $constant => $path )
{
if(!defined( strtoupper($constant) ) )
{
// define( strtoupper($constant), $path . '/');
define( strtoupper($constant), realpath( dirname( __FILE__) ) . $path . '/');
}
}
}
youpage.php
<?php
/**Create array of paths array of $constant to $path;
* i.e $path = array( 'THIS_CONSTANT' => '/this/path', 'WEB_ROOT' => '/path/to/webroot' );
* usage:
* `createPath( $path );`
* Test: `echo WEB_ROOT;` OUTPUT: '/path/to/webroot/'
*
* - How to Include another scripts:
* require_once CORE_PATH . 'Config.php';
* require_once INCLUDE_PATH . 'Database.php';
* require_once LAYOUT_PATH 'Header.php';
* require_once LAYOUT_PATH 'Body.php';
* require_once LAYOUT_PATH 'Footer.php';
*/
$path = array(
'SITE_ROOT' => $_SERVER['DOCUMENT_ROOT'],
'CORE_PATH' => '/core',
'INCLUDE_PATH' => '/inc',
'LAYOUT_PATH' => '/layout',
'BLOCK_PATH' => '/blocks',
'STATIC_PATH' => '/static'
);
//usage:
createPath( $path );
// Test. You can echo path, include | require e.g:
echo STATIC_PATH;
function createPath( $path )
{
if( empty( $path ) )
{
die("Array of path required!");
}
foreach( $path as $constant => $path )
{
if(!defined( strtoupper($constant) ) )
{
// define( strtoupper($constant), $path . '/');
define( strtoupper($constant), realpath( dirname( __FILE__) ) . $path . '/');
}
}
}
希望这有帮助!