php包含require文件

时间:2014-08-03 16:23:11

标签: php include require

我有一个我需要加载的php文件才能声明一些数组:

main conf-file.php

/* Defined main names, php files and action */
function __construct() {
    define( 'TINYMCE_DIR', plugin_dir_path( __FILE__ ) .'tinymce' );
    define( 'VISUAL_DIR', plugin_dir_path( __FILE__ ) .'visual' );  
    include( TINYMCE_DIR . '/shortcodes-config.php' );
    require_once( TINYMCE_DIR . '/shortcodes-popup.php' );
    require_once( VISUAL_DIR . '/visual.php' );   
}

shortcodes-config.php:

$shortcodes['video_section'] = array(
    'no_preview' => true,
    'params' => 'xxx',
    'shortcode' => '[sc1][/sc1]',
    'popup_title' => __('Video Section', THEME_NAME),
    'shortcode_icon' => __('li_video')
);

$shortcodes['image_section'] = array(
    'no_preview' => true,
    'params' => 'yyy',
    'shortcode' => '[sc2][/sc2]',
    'popup_title' => __('Image Section', THEME_NAME),
    'shortcode_icon' => __('li_image')
);

然后在其他一些文件中,我需要使用$shortcodes从数组中获取一些值。

visual.php:

require( TINYMCE_DIR . '/shortcodes-config.php' );// if don't requiered it's no working?
$icon  = $shortcodes['video_section']['shortcode_icon'];
$title = $shortcodes['video_section']['popup_title'];
$icon  = "<i class='fa ". $icon ."'></i>";
$icon .= "<span class='element-title'>". $title ."</span>";
return $icon;

的简-popup.php:

add_action('wp_ajax_display', 'display_shortcode_list_callback');
function display_shortcode_list_callback() {
  require( TINYMCE_DIR . '/shortcodes-config.php' );
  $title = $shortcodes['video_section']['popup_title'];
}

我不明白如何正确加载我的php文件然后使用我的数组值。 事实上如果我加载我的shortcodes-config.php不止一次,它就不能正常工作......

我可以使用file1或file2只使用一次。

包含一个php文件来声明某个变量的正确方法是什么,而不是之后得到它?

1 个答案:

答案 0 :(得分:1)

由于您已在require_once初步使用main conf-file.php,因此您无法再次要求:

require_once( TINYMCE_DIR . '/shortcodes-popup.php' );

解决方案是使用requireinclude