PHP Classes

Nice overkill.

Recommend this page to a friend!

      PHP Compare Objects  >  All threads  >  Nice overkill.  >  (Un) Subscribe thread alerts  
Subject:Nice overkill.
Summary:Package rating comment
Messages:5
Author:Artur Graniszewski
Date:2013-04-12 11:59:31
Update:2013-04-18 13:07:58
 

Artur Graniszewski rated this package as follows:

Utility: Bad
Consistency: Good
Examples: Good

  1. Nice overkill.   Reply   Report abuse  
Picture of Artur Graniszewski Artur Graniszewski - 2013-04-12 11:59:31
Nice overkill. You reimplemented sth already done in pure PHP:

<?php

class class1
{
private $x;
private $y;

function __set($propiedad,$valor)
{
$this->$propiedad=$valor;
}
function __get($propiedad)
{
return $this->$propiedad;
}
}

$object1=new class1();
$object1->x=10;
$object1->y=20;

$object2=new class1();
$object2->x=10;
$object2->y=20;

// returns true
var_dump($object1 == $object2);

$object3=new class1();
$object3->x=10;
$object3->y=10;

// returns false
var_dump($object1 == $object3);


  2. Re: Nice overkill.   Reply   Report abuse  
Picture of Jorge Prado Jorge Prado - 2013-04-17 17:55:21 - In reply to message 1 from Artur Graniszewski
That's already in PHP but you can not use var_dump to compare objects and get a result and assign it to a variable, there is not internally in PHP to compare objects because if I do this:

if($object1 == $object3)
echo "notequals";
else
echo "EQUALS";

It will always show "Equals". But if you use the class you'll receive false in this case.




  3. Re: Nice overkill.   Reply   Report abuse  
Picture of Artur Graniszewski Artur Graniszewski - 2013-04-18 08:49:46 - In reply to message 2 from Jorge Prado
I'm afraid I don't understand you.

PHP has already built-in functionality for comparing objects (it's been greatly improved in PHP5):
php.net/manual/en/language.oop5.obj ...

If you want to assign the boolean value to the variable then do sth like this:

$areObjectsEqual = ($object1 == $object2); // parethesis are optional here



  4. Re: Nice overkill.   Reply   Report abuse  
Picture of Artur Graniszewski Artur Graniszewski - 2013-04-18 08:54:00 - In reply to message 3 from Artur Graniszewski
BTW, you have error in your code, it should look like this:

if($object1 == $object3)
echo "EQUALS";
else
echo "not equals";

(you echo'ed "not equals" in case of ==).

  5. Re: Nice overkill.   Reply   Report abuse  
Picture of Jorge Prado Jorge Prado - 2013-04-18 13:07:58 - In reply to message 4 from Artur Graniszewski
You are right, I was wrong.