Posted By

stancell on 04/21/08


Tagged

object equals compare interface


Versions (?)

Object equality comparison (comparable equals interface)


 / Published in: PHP
 

Sample which shoows how to compare objects by content.

  1. interface Comparable_Equals
  2. {
  3. public function equals(self $other);
  4. }
  5.  
  6.  
  7. class User
  8. implements Comparable_Equals
  9. {
  10. public function equals(self $other)
  11. {
  12. return $this['id'] == $other['id'];
  13. }
  14. // other methods
  15. }
  16.  
  17. $stancell = User::loadByUsername('stancell');
  18. $user = User::loadById(20);
  19.  
  20. echo $user->equals($stancell) ? 'The same user' : 'Different user';

Report this snippet  

You need to login to post a comment.