PHP Classes

Why use native define?

Recommend this page to a friend!

      Constant Array 2  >  All threads  >  Why use native define?  >  (Un) Subscribe thread alerts  
Subject:Why use native define?
Summary:no point in using define() if you're using var_export
Messages:3
Author:rudie dirkx
Date:2011-02-09 14:50:07
Update:2011-03-03 19:12:47
 

  1. Why use native define?   Reply   Report abuse  
Picture of rudie dirkx rudie dirkx - 2011-02-09 14:50:07
Why do you use the native define() to define global constants? You're not using them like you should anyway, due to the var_export, so why not do it right?

<?php

class Const {
$constants = array();
static function set($name, $var) {
self::$constants[$name] = $var; // yup, that easy
}
static get($name, $alt = null) {
return isset(self::$constants[$name]) ? self::$constants[$name] : $alt;
}
}

?>

  2. Re: Why use native define?   Reply   Report abuse  
Picture of Ville Walveranta Ville Walveranta - 2011-03-03 19:03:03 - In reply to message 1 from rudie dirkx
The whole point of this class is to be able to save an array into a _constant_ (hence define() is used).

You also don't want to return a null because, technically, 'null' could be a value of a property in an array.

  3. Re: Why use native define?   Reply   Report abuse  
Picture of Ville Walveranta Ville Walveranta - 2011-03-03 19:12:47 - In reply to message 2 from Ville Walveranta
Can't edit messages here, so I elaborate in another message:

I should've said above: "You also don't want to return a null by default when a property is not found because, technically, 'null' could be the value of a property in an array."