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

chr*_*lee 15 php

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

Notice: Use of undefined constant __DIR__ - assumed '__DIR__
Run Code Online (Sandbox Code Playgroud)

这是因为我正在使用魔法常数__DIR__.有没有替代使用__DIR__5.3或更少?

这是导致它的代码:

<?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);
Run Code Online (Sandbox Code Playgroud)

Tch*_*upi 46

使用旧技巧:

dirname(__FILE__)
Run Code Online (Sandbox Code Playgroud)

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

  • @chrisjlee 是的,它产生相同的结果 (2认同)