Bug #72483 [Opn->Dup]: General error with inheritance/implementation and returning type

From: Date: Fri, 24 Jun 2016 12:16:46 +0000
Subject: Bug #72483 [Opn->Dup]: General error with inheritance/implementation and returning type
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201825@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72483&edit=1

 ID:                 72483
 Updated by:         cmb@php.net
 Reported by:        alan dot g-l at live dot fr
 Summary:            General error with inheritance/implementation and
                     returning type
-Status:             Open
+Status:             Duplicate
 Type:               Bug
 Package:            Reproducible crash
 Operating System:   Debian 8.5
 PHP Version:        7.0.8
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

Duplicate of bug #72442.


Previous Comments:
------------------------------------------------------------------------
[2016-06-24 06:51:58] alan dot g-l at live dot fr

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 this bug report at https://bugs.php.net/bug.php?id=72483&edit=1


Thread (2 messages)

« previous php.bugs (#201825) next »