Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts

    Force in_array to use strict comparison?

    Hi all,

    I'm using PHP 5.2.

    It would seem that the in_array built-in (and other comparison builtins) use non-strict comparison (==) rather than strict comparison (===).

    Is it possible to force the use of strict comparison locally? (e.g. I would like to switch strict comparison on, call in_array and then switch strict comparison off).

    I suppose the obverse of this question is, can you overload the == operator? You can do this in C++ (you can overload pretty much anything in C++!)

    TIA,

    Mike.
    Last edited by mlse; 08-02-2007 at 03:05 PM.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    The method signature of in_array is:
    bool in_array ( mixed $needle, array $haystack [, bool $strict] )

    and the manual page has the following to say:

    "If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack."

    Is that what you're after?
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,076
    Thanks
    2
    Thanked 320 Times in 312 Posts
    Can you give an example of some specific data and what the expected results are vs what you are getting now?

    You won't be able to change the operation of any of the PHP built in functions, unless it has a switch/parameter to do so or you write your own function with the checking you need.

    Edit: LOL, I see GJay posted while I was typing this. I see this was a case of RTFM to find out how to use the function properly.
    Last edited by CFMaBiSmAd; 08-02-2007 at 03:55 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Hi guys,

    I DID RTFM!

    What I'm after is a way of making those comparison methods perform more than just type checking; I want to be able to overload == to make it behave like === (so that in_array will perform an identically-equals check rather than just a would-print-the-same-as check).

    I've written a noddy function to do it, just wondered if there was a built-in to do it!

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    do you have an example of where your function behaves differently to in_array() with strict=true?
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #6
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Hi GJay,

    Yep, I have created a class which is self-referential; i.e. it contains a protected member variable which can point to NULL or to a reference to the instance that owns it.

    For example:

    PHP Code:
    public function foo()
    {
      
    $this->m_selfref =& $this;

    When I call in_array($inst, $arr, TRUE) where $inst is an instance of my self-referential class (for which foo has been called) and $arr is an array which contains one or more instances of the same class (also for which foo has been called), I get an out-of-memory error, which suggests that in_array is using == to evaluate $inst against each member of $arr.

    My alternative method is:

    PHP Code:
    function in_array_identical(Cselfref $inst, array $arr)
    {
      
    $values array_values($arr);

      foreach (
    $values as $value)
        if (
    $value === $inst)
          return 
    true;

      return 
    false;

    This seems to work just fine!

    Mike.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •