Asked  7 Months ago    Answers:  5   Viewed   49 times

I'm attempting to create a function with flags as its arguments but the output is always different with what's expected :

define("FLAG_A", 1);  
define("FLAG_B", 4);  
define("FLAG_C", 7);  
function test_flags($flags) {  
 if($flags & FLAG_A) echo "A";  
 if($flags & FLAG_B) echo "B";  
 if($flags & FLAG_C) echo "C";   
}  
test_flags(FLAG_B | FLAG_C); # Output is always ABC, not BC  

How can I fix this problem?

 Answers

70

Flags must be powers of 2 in order to bitwise-or together properly.

define("FLAG_A", 0x1);
define("FLAG_B", 0x2);
define("FLAG_C", 0x4);
function test_flags($flags) {
  if ($flags & FLAG_A) echo "A";
  if ($flags & FLAG_B) echo "B";
  if ($flags & FLAG_C) echo "C";
}
test_flags(FLAG_B | FLAG_C); # Now the output will be BC

Using hexadecimal notation for the constant values makes no difference to the behavior of the program, but is one idiomatic way of emphasizing to programmers that the values compose a bit field. Another would be to use shifts: 1<<0, 1<<1, 1<<2, &c.

Wednesday, March 31, 2021
 
eek
answered 7 Months ago
eek
40

Since you're trying to test an external API, I would begin by wrapping your file_get_contents() call in another class and injecting that into your BaseClient. In the simplest form, it might look something like this:

class RemoteFileRetriever
{
    public function retrieveFileContents($url)
    {
        // Do some work to create $context
        ...

        // Now grab the file contents
        $contents = file_get_contents($url, false, $context);

        return $contents;
    }
}

abstract class BaseClient
{
    private $fileRetriever;

    public function __construct(RemoteFileRetriever $fileRetriever)
    {
        $this->fileRetriever = $fileRetriever;
    }

    protected function call($method, array $params = array())
    {
        ...

        $file = $this->fileRetriever->retrieveFileContents($this->getDSN());

        ...
    }
}

Now in your test, you can use a mock object to inject as the file retriever. E.g.:

class ClientTest extends PHPUnit_FrameWork_TestCase
{
    public function testTestCall()
    {
        $mockRetriever = new MockRemoteFileRetriever();
        $c = new Client($mockRetriever);
        $resp = $c->testCall('Hello World');

        $this->assertEquals('Hello World', $resp);
    }
}

PHPUnit atually has some built-in helpers for mocking. See PHPUnit's Mock Objects.

Saturday, May 29, 2021
 
LukeP
answered 5 Months ago
48

The ~ unary operator is bitwise negation. If you need fewer bits than what fits in an int then you'll need to mask it with & after the fact.

Tuesday, June 29, 2021
 
Arman
answered 4 Months ago
79

Benefits of using Flags enum:

  • Standard approach: "They are the correct design to use when multiple enumeration values can be specified at the same time."
  • Intent is clear
  • Maintainable -- new programmers should pick this up easily
  • Easily extensible -- support for new flag combinations (e.g. weekend)
  • Fast

Negatives of using Flags enum:

  • Data representation for humans hard to understand (e.g. what flags are set for 17?)


Benefits of using string of bits:

  • Easy for programmers to see which bits are set in string

Negatives of using string of bits:

  • Non-standard approach
  • Harder to understand for programmers unfamiliar with your design
  • Potentially easier to set "garbage" values (e.g. stringValue = "Sunday")
  • Needless string creation
  • Needless string parsing
  • Additional development work
  • Reinventing the wheel (but not even a round wheel)


How important is it really to be able to look at the string of bits to see what is set? If it's hard to know that 17 is Monday and Friday, you can always use calculator and convert to binary. Or add some sort of string representation for "display" (or debugging) use. It's not that difficult.


It also seems to me that if you are going to make the string of bits approach solid then you will need to do quite a bit of encapsulation to bring it up to a level of abstraction that the Flags enum already provides. If the approach is to simply manipulate the string of bits directly then that is going to be hard to read (and understand) and probably error prone.

e.g. you may end up seeing this:

days = "1000101"; // fixed bug where days were incorrectly set to "1010001"
Sunday, August 8, 2021
 
Litty
answered 3 Months ago
22

add these code into the top of your script

@set_magic_quotes_runtime(false);
ini_set('magic_quotes_runtime', 0);
Wednesday, August 18, 2021
 
Warrior
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :