繁体   English   中英

PHP的包括要求文件

[英]php include require file

我有一个PHP文件需要加载以声明一些数组:

主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文件来声明一些变量而不是在获取变量之后的正确方法?

因为您最初在main conf-file.php文件中使用了require_once ,所以不允许再次要求它:

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

解决方案是使用requireinclude

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM