Remove lang.Object class
thekid opened this issue · 2 comments
thekid commented
Scope of Change
This RFC suggests removing the lang.Object
class
Rationale
This: https://wiki.php.net/rfc/object-typehint
Functionality
The class Object
in the lang
package will be removed, as its declaration will cause a compile error.
Object vs. Value
Classes often extend lang.Object
to:
- Inherit its default
toString()
method. - Be useable inside hash contexts by inheriting its
hashCode()
method - Provide (and possibly override)
equals()
, which is also inherited
It's easier to inherit and overwrite instead of implementing lang.Value
fully.
Traits
A solution to this will be to add traits to implement lang.Value
by default. Classes can then include these and overwrite whatever necessary.
<?php namespace lang;
trait StringOf {
public function toString(): string {
return nameof($this).'@'.Objects::stringOf(get_object_vars($this));
}
}
trait HashOf {
public function hashCode(): string {
return Objects::hashOf(array_merge([get_class($this)], (array)$this));
}
}
trait Comparison {
public function compareTo($value): int {
return $value instanceof self ? Objects::compare((array)$this, (array)$value) : 1;
}
}
Security considerations
n/a
Speed impact
Not measurable
Dependencies
n/a
Related documents
xp-framework/unittest#19 - example refactoring
thekid commented
Timm@SLATE %home%\devel\xp\core [master]
$ grep -HErn "extends.+Object" src|wc -l
128
thekid commented
See https://travis-ci.org/xp-forge/web/jobs/248532308
Uncaught error: Compile error (Cannot use 'Object' as class name as it is reserved)
at <source> [line 9 of ./vendor/xp-framework/core/src/main/php/lang/Object.class.php]
at <main>('xp.unittest.TestRunner', 'src/test/php') [line 0 of .../class-main/php.class.php]