rfc:strict_operators:faq

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:strict_operators:faq [2020/06/10 12:28] jasnyrfc:strict_operators:faq [2020/07/06 13:46] jasny
Line 13: Line 13:
 Making significant changes to the behavior of operators has severe consequences to backward compatibility. Additionally, there is a significant group of people who are in favor of the current method of type juggling. Following the rationale of [[rfc/scalar_type_hints_v5|PHP RFC: Scalar Type Declarations]]; an optional directive ensures backward compatibility and allows people to choose the type checking model that suits them best. Making significant changes to the behavior of operators has severe consequences to backward compatibility. Additionally, there is a significant group of people who are in favor of the current method of type juggling. Following the rationale of [[rfc/scalar_type_hints_v5|PHP RFC: Scalar Type Declarations]]; an optional directive ensures backward compatibility and allows people to choose the type checking model that suits them best.
  
-==== Why does == and != only support int and float operands ====+==== Why does == and != only support int and float operands====
  
 The main difference between the equal (''%%==%%'') and identical (''%%===%%'') operator, is that ''%%==%%'' doesn't perform type casting. By only disabling type casting with strict_operators, ''%%==%%'' would be similar to ''%%===%%'' for scalar types. The main difference between the equal (''%%==%%'') and identical (''%%===%%'') operator, is that ''%%==%%'' doesn't perform type casting. By only disabling type casting with strict_operators, ''%%==%%'' would be similar to ''%%===%%'' for scalar types.
Line 57: Line 57:
 Strict comparison of arrays as unsorted hashmaps currently isn't possible and requires sorting the array, prior to comparison. Strict comparison of arrays as unsorted hashmaps currently isn't possible and requires sorting the array, prior to comparison.
  
-With ''%%==%%'' unavailable when using strict_operators, sorting the array would be the only option available.+With ''%%==%%'' unavailable when using strict_operators, sorting the array is the only option available. 
 + 
 +<code php> 
 +ksort($array1); 
 +ksort($array2); 
 + 
 +$array1 === $array2; 
 +</code>
  
 Array functions might be added to compare arrays in different ways. But that's outside the scope of this RFC. Array functions might be added to compare arrays in different ways. But that's outside the scope of this RFC.
 +
 +==== How can objects be compared by property? ====
 +
 +With the ''=='' operator, two object instances are equal if they have the same attributes and values, and are instances of the same class. Values are compared with ''=='', applying type juggling. Using ''==='' will check if the objects are the same instance.
 +
 +When using strict_operators, ''=='' with objects will always throw a ''TypeError''. With ''%%==%%'' unavailable, ''get_object_vars'' needs to be used.
 +
 +
 +<code php>
 +get_class($object1) === get_class($object2) && get_object_vars($object1) === get_object_vars($object2);
 +</code>
 +
  
 ==== Why isn't is allowed to increment strings with strict_operators? ==== ==== Why isn't is allowed to increment strings with strict_operators? ====
rfc/strict_operators/faq.txt · Last modified: 2020/07/06 15:01 by jasny