Bug #72483 [NEW]: General error with inheritance/implementation and returning type
| From: | alan dot g-l at live dot fr | Date: | Fri, 24 Jun 2016 06:52:02 +0000 |
| Subject: | Bug #72483 [NEW]: General error with inheritance/implementation and returning type | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-201818@lists.php.net to get a copy of this message | ||
From: alan dot g-l at live dot fr
Operating system: Debian 8.5
PHP version: 7.0.8
Package: Reproducible crash
Bug Type: Bug
Bug description:General error with inheritance/implementation and returning type
Description:
------------
If you want to build a function that force to return its own instance,
you simply set it as returning self.
But when you want to extend it, then PHP will said that our new class is
not compatible with the parent one if we are overloading it.
So maybe the "static" keyword is the answer, as it will be evaluated
when executing? Wrong, PHP does not except the "static" keyword.
This problems are the same for both class inheritance and interface
implementation.
Test script:
---------------
// Interfaces
// Sample 1
// with "self" keyword
interface MyInterface {
public function myFunction(): self;
}
class MyClass implements MyInterface{
public function myFunction(): self {
return $this;
}
}
// Fatal error: Declaration of MyClass::myFunction(): MyClass must be
compatible with MyInterface::myFunction(): MyInterface
// Sample 2
// with "static" keyword
interface MyInterface {
public function myFunction(): static;
}
class MyClass implements MyInterface {
public function myFunction(): static {
return $this;
}
}
// Parse error: syntax error, unexpected 'static' (T_STATIC)
// Classes and inheritance
// Sample 3
// with "self" keyword
class MyClass {
public function myFunction(): self {
return $this;
}
}
class MyClass2 extends MyClass {
public function myFunction() : self {
return $this;
}
}
// Fatal error: Declaration of MyClass2::myFunction(): MyClass2 must be
compatible with MyClass::myFunction(): MyClass
// Sample 4
// with "static" keyword
class MyClass {
public function myFunction(): static {
return $this;
}
}
class MyClass2 extends MyClass {
public function myFunction(): static {
return $this;
}
}
// Parse error: syntax error, unexpected 'static' (T_STATIC)
Expected result:
----------------
I think that the "self" keyword should work, as MyClass implements
MyInterface (or MyClass2 extends MyClass), so the first one is an
instance of the second one.
Anyway, we can imagine that my asumption is false. So why that "static"
word would not work here?
--
Edit bug report at https://bugs.php.net/bug.php?id=72483&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72483&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72483&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72483&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=72483&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=72483&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=72483&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=72483&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=72483&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=72483&r=support
Expected behavior: https://bugs.php.net/fix.php?id=72483&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=72483&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=72483&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=72483&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72483&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=72483&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=72483&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=72483&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72483&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=72483&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=72483&r=mysqlcfg