1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Alternative to... Bitwise operators in XF...?

Discussion in 'XenForo Development Discussions' started by Robust, Nov 27, 2015.

  1. Robust

    Robust Well-Known Member

    I'm no PHP expert so I'm not really someone to be criticising if others are getting along with this fine, but now I think it's usual to use query builders to access the DB. I think Laravel is a perfect example of good approaches to modern PHP approaches.


    It's much easier to work with that, and I can do what takes a lot of lines in XF (to do properly, anyway).

    So, is there any other way in XF 1.5 to do what I'm looking to do (will describe below). I'm assuming (and hoping) that XF 2.0 will have significant improvements to developer workflow, reducing unnecessary work, using modern approaches and reducing boilerplate and repetition of code, but until then...

    My aim is to access the same table to get something. So, say for action 1 I need to get just general info from the table like the title and description. In action 2, I need to get title, description, more data like user IDs etc. In action 3, I need to get the info in action 2, plus left join about 4-5 other tables in to fetch specific data from the unique IDs stored in the table I got the title and description from. In action 4 I need to get the data in action 2, but fetch the user too.

    So really, it's just the case of in some actions/controller functions I need to get more data, others I need more joins to fetch data associative to the IDs that already exist.

    Right now, I'd probably be expected to compare bitwise operators but bits are beyond something I know and really I don't want to bother work with them if they're outdated technologies that aren't really used anymore. There's probably more modern and nicer approaches to it. I mean, there's always check if a string is set and if it is do an action, but something that doesn't involve that action at all, possibly.

    Thanks :)
  2. thedude

    thedude Well-Known Member

    As @Mike mentioned in a different thread about what's coming up for XF 2.0, there will be some sort of query builder developers can use:

    It's never a bad thing to learn computer science fundamentals such as bitwise operators, especially if you're a programmer They're definitely not "outdated technologies that aren't really used anymore".
    Luke F likes this.
  3. Robust

    Robust Well-Known Member

    Praise the lord.

    Yeah... I dunno. Maybe not completely useless, but I have no use for knowing bits in depth and bitwise operators with comparisons of bits, to be honest.
  4. Jake B.

    Jake B. Well-Known Member

    Quick dirty (but very simple) bitwise operator tutorial -- pretty much sums up anything you'll need to know with XenForo's models at least:

    Say you have 4 constants:
    const JOIN_A 0x01// 001
    const JOIN_B 0x02// 010
    const JOIN_C 0x04// 100
    const JOIN_D 0x03// 011 -- You really shouldn't use this as it could potentially cause issues.
    When setting the joins you want to use you use the Or (|) operator which sets the bits in both the first and second variable so if you do:
    $join self::JOIN_A self::JOIN_B// 011
    $anotherJoin self::JOIN_Cself::JOIN_D self::JOIN_A// 111
    Then when doing the check you'd use the And (&) operator in an if statement so:
    if ($join self::JOIN_C// This should be 'false' because the '4' bit isn't set in $join, just the 1 and 2 bit
    if ($anotherJoin self::JOIN_B// would return true because the 2 bit is set in $anotherJoin
    For what XenForo's doing by default that's all you really need to know as far as bitwise operators come. That and of course a very basic understanding of at least how binary works :)
    Daniel Hood, Robust and thedude like this.
  5. Robust

    Robust Well-Known Member

    Thanks! So what about making them unique? Like 0x03 would apply to both 0x01 and 0x02 (bits are present). So for unique ones do I just double? So after 0x04 it'd be 0x08, 0x16, 0x32, etc?
  6. Robust

    Robust Well-Known Member

    @Jake B. What about this, too:

    const FETCH_ASSOC = 0x02;
    const FETCH_ASSOC_EXTENDED = 0x04;

    I want fetch_assoc fetches associated data, and fetch_assoc_extended fetches extended assoc data. How can I have fetch_assoc_extended also include fetch_assoc?
  7. thedude

    thedude Well-Known Member

    You can pass multiple using a bitwise OR which is this: |

    So it would be

    Good little explanation here about bitwise OR and AND vs logical OR and AND
  8. Jake B.

    Jake B. Well-Known Member

    If you did 0x03 you would have 11 in binary so it'd cover both 0x01 and 0x02. For unique you'd use any base 2 number (Anything 2^x -- 2, 4, 8, 16, 32, etc. -- any number that has it's own bit)

Share This Page