Re: Re[6]: Need a more elegant way of bitwise ORing values

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2007-06-13 at 15:19 +0100, Richard Davey wrote:
> Hi Robert,
> 
> Wednesday, June 13, 2007, 3:15:39 PM, you wrote:
> 
> > It's terribly verbose and inefficient...
> 
> > <?php
> 
> > $filter['flags'] = 0;
> 
> > if( $allow_fraction )
> > {
> >     $filter['flags'] |= FILTER_FLAG_ALLOW_FRACTION;
> > }
> 
> > if( $allow_thousand )
> > {
> >     $filter['flags'] |= FILTER_FLAG_ALLOW_THOUSAND;
> > }
> 
> > if( $allow_scientific )
> > {
> >     $filter['flags'] |= FILTER_FLAG_ALLOW_SCIENTIFIC;
> > }
> 
> ?>
> 
> I don't think it's *terribly* verbose, as it has good sentence structure
> to it, but your version is certainly more efficient, hence I've
> swapped to that. Any other takers? ;)

Personally I hate constants (can't use non-scalar values so why get used
ot them... also they're just another point for name collision) so if it
were my own code I'd do something more like the following:

<?php

    $GLOBALS['filterFlags'] = array
    (
        'allowFraction'   => 1 << 0,
        'allowThousand'   => 1 << 1,
        'allowScientific' => 1 << 2,
    );

    // then your above code would become:

    $filter['flags'] = 0;
    foreach( $filterFlags as $name => $bits )
    {
        if( isset( $$name ) && $$name )
        {
            $filter['flags'] |= $bits;
        }
    }

?>

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux