【问题标题】:php include require filephp包含需要文件
【发布时间】:2014-08-03 16:23:11
【问题描述】:

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

主配置文件.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' );   
}

简码-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 文件来声明某个变量而不是在之后获取它的正确方法是什么?

【问题讨论】:

  • 你为什么会多次加载它
  • 因为之后我无法再访问它了...我认为这是因为我在 ajax 中使用过一次,然后在正常情况下再次使用它...
  • 你需要调试这个添加ini_set('display_errors', 1);error_reporting(E_ALL);到你的文件

标签: php include require


【解决方案1】:

因为你已经在main conf-file.php 中使用了require_once,所以你不能再次要求它:

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

解决方案是使用requireinclude

【讨论】:

    猜你喜欢
    • 2011-12-15
    • 2016-09-02
    • 2013-10-23
    • 1970-01-01
    • 2013-10-10
    • 2014-08-10
    • 2019-09-09
    • 2012-07-03
    相关资源
    最近更新 更多