Re: Re: question about defining constants and pear coding standards

From: Date: Mon, 25 Sep 2006 04:52:54 +0000
Subject: Re: Re: question about defining constants and pear coding standards
References: 1 2 3 4 5  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-44045@lists.php.net to get a copy of this message
Justin Patrin wrote:
...and second... what happens if the $mode isn't explicitly specified? I'd like Some_Object to use a default mode at that point, however, I'm not so sure this would be possible using this method. With getStaticProperty, an undefined index will be referenced, in this scenario. If the error reporting is set to E_ALL, this would result in an error. I could surpress the error with a @, but this doesn't seem to be a particurarly elegant solution, regardless.
No, of course it wouldn't, as your code would check to see if it's set and if it's not it would use a default. if (isset($options['mode'])) So, basically, you're recommending I do this?:
(test.php): <?php define('SOME_OBJECT_MODE_INTERNAL',1); define('SOME_OBJECT_MODE_EXTERNAL',2); include('PEAR.php'); class Some_Object { function Some_Object() {
       $mode = PEAR::getStaticProperty('Some_Object', 'mode');
       switch ($mode) {
           case SOME_OBJECT_MODE_INTERNAL:
               echo 'internal';
               break;
           case SOME_OBJECT_MODE_EXTERNAL:
               echo 'external';
       }
} } ?> If so, the problem with this is that although... (test2.php): <? include('test.php'); $mode =& PEAR::getStaticProperty('Some_Object','mode'); $mode = SOME_OBJECT_INTERNAL; $a = new Some_Object(); ?> ...works, ... (test2.php): <? include('test.php'); $a = new Some_Object(); ?> ...doesn't. I would like - if no mode is explicitly defined for a default mode to be used. If I do this, using your suggestion, I'll get an error in the third code snippet (unless I supress error messages with @, and I'd rather not use a cludge). The reason I'd get an error message should be clear, giving the following (from PEAR.php): function &getStaticProperty($class, $var) {
       static $properties;
       return $properties[$class][$var];
} In the third code snippet, $properties['Some_Object']['mode'] will be referenced, even though it was never defined. This will cause an error if E_ALL is set. And I don't see how doing an isset is going to help, either. I can't do it before getStaticProperty is called, because I wouldn't have anything to do it on, and if I do it after getStaticProperty, it's too late, since the error would be caused from within getStaticProperty (perhaps getStaticProperty should do an isset check?) Now, contrast the above with this: (test.php): <?php define('SOME_OBJECT_MODE_INTERNAL',1); define('SOME_OBJECT_MODE_EXTERNAL',2); class Some_Object { function Some_Object() {
       if ( defined('SOME_OBJECT_MODE') ) {
           switch (SOME_OBJECT_MODE) {
               case SOME_OBJECT_MODE_INTERNAL:
                   echo 'internal';
                   break;
               case SOME_OBJECT_MODE_EXTERNAL:
                   echo 'external';
           }
       } else {
           echo 'internal';
       }
} } ?> (test2.php): <? include('test.php'); define('SOME_OBJECT_MODE',SOME_OBJECT_MODE_EXTERNAL); $a = new Some_Object(); ?> or... (test2.php): <? include('test.php'); $a = new Some_Object(); ?> Basically, Some_Object will use default mode even if none is explicitly set. I don't see how your suggestion would allow me to do this and work while E_ALL is set and without error suppression. Unless I'm missing something?

« previous php.pear.dev (#44045) next »