PHP 5.2注意:使用未定义的常量__DIR__ - 假设'__DIR__

时间:2013-02-01 19:52:34

标签: php

在php 5.3或更低版本中,它会提供如下错误:

Notice: Use of undefined constant __DIR__ - assumed '__DIR__

这是因为我正在使用魔法常数__DIR__。是否有在5.3或更少的情况下使用__DIR__的替代方法?

以下是导致它的代码:

<?php
/**
 * Load template files
 *
 * $files   Contains alphabetized list of files that will be required
 */
$files = array(
  'elements.inc',
  'form.inc',
  'menu.inc',
  'theme.inc',
);

function _zurb_foundation_load($files) {
  $tp = drupal_get_path('theme', 'zurb_foundation');
  $file = '';

  // Workaround for magic constant; for now because of php 5.2 issue
  // http://drupal.org/node/1899620#comment-6988766
  if( !defined( __DIR__ ) )define( __DIR__, dirname(__FILE__) );

  // Check file path and '.inc' extension
  foreach($files as $file) {
    $file_path = __DIR__ .'/inc/' . $file;
    if ( strpos($file,'.inc') > 0 && file_exists($file_path)) {
      require_once($file_path);
    }
  }
}

_zurb_foundation_load($files);

1 个答案:

答案 0 :(得分:46)

使用旧技巧:

dirname(__FILE__)

但如果可能,请更新到较新版本的PHP。