10.2. Static Methods

There are several static methods available from the Xyster_Type class that assist you in working with types and equality.

10.2.1. Testing Value Equality

There are two methods for determining if two objects are equal to each other.

10.2.1.1. areEqual

This method will return true if two values are identical or shallowly equal. Objects of the same class but are not identical are not considered to be equal by this method even if they share the exact same values.

<?php
Xyster_Type::areEqual('foo', 'foo'); // true
Xyster_Type::areEqual('foo', 'bar'); // false
Xyster_Type::areEqual(new ArrayObject, new ArrayObject); // false
$object = new ArrayObject;
Xyster_Type::areEqual($object, $object); // true

If the values aren't identical and if the first value you supply is an object and it has an equals method, the result of this method will be returned. In this way, you can write your own logic for determining object equality.

<?php
class SmartEquals
{
    protected $_value;
    
    public function __construct( $value )
    {
        $this->_value = $value;
    }
    
    public function equals( $value )
    {
        return $value === $this || ( get_class($this) == get_class($value) &&
                $this->_value == $value->_value); 
    }
}
Xyster_Type::areEqual(new SmartEquals('foobar'), new SmartEquals('foobar')); // true 

10.2.1.2. areDeelyEqual

This method is similar to areEqual, but it also will compares values deeply.

If two objects are not identical (that is, are exactly the same instance) they will be compared property-by-property recursively if need be. This method will not nest any deeper than 10 levels to compare objects. Arrays will be compared in the same manner.

Just like areEqual, if the values aren't identical and if the first value you supply is an object and it has an equals method, the result of this method will be returned. The same is true for any values that are compared recursively.

10.2.2. Hash Codes

A hash code is a signed integer that identifies an object or a value. Developers of other languages like Java are no strangers to hash codes. The general rule is that if two objects are equal, their hash codes will be equal (the reverse is not always true).

A hash code should not be any greater than PHP_INT_MAX or smaller than the negative value of PHP_INT_MAX, or else PHP will turn it into a float. The hash number for a string returned from Xyster_Type should be the same as the hash for the same string from Java.

<?php
Xyster_Type::hash('hello'); // 99162322
Xyster_Type::hash('helloa'); // -1220935217
Xyster_Type::hash('hello to the world'); // -1821194164
Xyster_Type::hash(123.123); // 1123434234
Xyster_Type::hash(true); // 1231
Xyster_Type::hash(true); // 1237
Xyster_Type::hash(456789); // 456789

By default, any instance of an object will return a unique hash code. The hash method computes an integer based on the return value of spl_object_hash.

<?php
$obj = new ArrayObject;
$obj2 = new ArrayObject;
Xyster_Type::hash($obj);  // not the same as...
Xyster_Type::hash($obj2);

If you supply an object that has a hashCode method, the return value of this method will be returned. In this way, you can write your own hash calculations for your objects.

<?php
class SmartHash extends SmartEquals
{    
    public function hashCode()
    {
        return Xyster_Type::hash($this->_value);
    }
}
$obj = new SmartHash('hello');
$obj2 = new SmartHash('hello');
Xyster_Type::hash($obj);  // 99162322
Xyster_Type::hash($obj2); // 99162322

10.2.3. Parameter Types

The getForParameters method is just a convenience function to get an array of the types of the parameters for a method or function.

This method uses type hints and default values where it can to determine the types. If a type hint or default value is unavailable for a parameter, it will use a special type object representing scalar. If you pass any scalar type to the isInstance method of this object, it will return true.