14

In php 5.3 or less it'll provide an error like the following:

Notice: Use of undefined constant __DIR__ - assumed '__DIR__

It's because i'm using the magic constant __DIR__. Is there an alternative to using __DIR__ in 5.3 or less??

Here's the code that's causing it:

<?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);
8
  • 2
    Better idea: Update. PHP5.2 is not maintained anymore. And sooner or later and update is a good idea anyway ;)
    – KingCrunch
    Feb 1, 2013 at 19:55
  • @KingCrunch true i was thinking that from the get go; but it's like an internet explorer 6 problem. People don't want to give it up
    – chrisjlee
    Feb 1, 2013 at 19:56
  • 1
    IE6 is unsupported by MS itself, you know? ;) PHP5.2 must be treated as unsecure. On the other side 5.4 is out for a while now and even 5.5 will come somewhere between april and june. In my opinion it is not useful to give "this people" reasons to stick on 5.2
    – KingCrunch
    Feb 1, 2013 at 20:04
  • 1
    Glad i'm not the only one. Seems like there are 10,000 others with the same problem.
    – chrisjlee
    Jul 9, 2014 at 13:12
  • 1
    Thought this was actually a useful question.
    – Jahmic
    Mar 7, 2015 at 12:48

1 Answer 1

46

Use the old trick:

dirname(__FILE__)

But if possible, update to a newer version of PHP.

10
  • 1
    That's acceptable to use even after 5.3 ?
    – chrisjlee
    Feb 1, 2013 at 19:53
  • 2
    @chrisjlee Yes it produces the same result
    – Tchoupi
    Feb 1, 2013 at 19:54
  • 1
    "acceptable" maybe, but you should prefer __DIR__ for readability.
    – KingCrunch
    Feb 1, 2013 at 19:56
  • @KingCrunch On PHP 5.3+ I will use __DIR__ without hesitations. But for compatibility with earlier versions dirname(__FILE__) is not that ugly and widely used. And in my opinion dirname(__FILE__) is far more readable than if( !defined( __DIR__ ) )define( __DIR__, dirname(__FILE__) );
    – Tchoupi
    Feb 1, 2013 at 19:58
  • @MathieuImbert As mentioned in my comment to the question itself: 5.2 is unmaintained. Or can you buy modern games compatible to windows 98 nowadays? // I would keep this whole define()-crap away: If __DIR__ doesn't exists, the runtime is incompatible. Thats it.
    – KingCrunch
    Feb 1, 2013 at 20:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.