daily pastebin goal
5%
SHARE
TWEET

PHP Comparing Objects Bug

mstraczkowski Dec 13th, 2013 372 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * Simple abstract class with one
  5.  * protected property
  6.  */
  7. abstract class A
  8. {
  9.     /**
  10.      * An errors array property
  11.      * @var array
  12.      */
  13.     protected $errors = array();
  14. }
  15.  
  16. /**
  17.  * Simple class which extends A
  18.  */
  19. class B extends A
  20. {
  21.     /**
  22.      * An errors array property
  23.      * @var array
  24.      */
  25.     protected $errors = array();
  26.  
  27.     /**
  28.      * Value from constructor
  29.      * @var string
  30.      */
  31.     protected $value = null;
  32.  
  33.     /**
  34.      * Method stores value in property
  35.      *
  36.      * @access  public
  37.      * @param   string  $value  Value to store
  38.      * @return  void
  39.      */
  40.     public function __construct($value)
  41.     {
  42.         $this->value = $value;
  43.     }
  44. }
  45.  
  46. // Creating new instance of B with "ABC" string
  47. $object1 = new B('ABC');
  48.  
  49. // Creating new instance of B with "CBA" string
  50. $object2 = new B('CBA');
  51.  
  52. // Both objects got different value of $value property
  53. // They should be different, but PHP returns true here
  54. var_dump($object1 == $object2);
  55.  
  56. // Printing first object
  57. print_r($object1);
  58.  
  59. // Printing second object
  60. print_r($object2);
  61.  
  62. // Comparing objects again but now PHP returns false
  63. var_dump($object1 == $object2);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top