Object equality comparison (comparable equals interface)


/ Published in: PHP
Save to your folder(s)

Sample which shoows how to compare objects by content.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.