Bug #71119 [Nab]: Cannot override methods on extended classes

From: Date: Tue, 15 Dec 2015 14:00:44 +0000
Subject: Bug #71119 [Nab]: Cannot override methods on extended classes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197901@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71119&edit=1

 ID:                 71119
 Updated by:         nikic@php.net
 Reported by:        chazmead89 at gmail dot com
 Summary:            Cannot override methods on extended classes
 Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   Mac OSX 10.8.5
 PHP Version:        7.0.0
 Block user comment: N
 Private report:     N

 New Comment:

FYI "override" and "overload" are not the same thing. What you say would make
sense if this would create a method overload. However PHP does not support method overloading.


Previous Comments:
------------------------------------------------------------------------
[2015-12-15 09:00:41] chazmead89 at gmail dot com

What about this test case then, called Method overloading..

class A {
  public function foo(array $bar) { print_r($bar); }
}

class B extends class A {
  public function foo(array $bar, string $baz) { print_r([$bar, $baz]); }
}

Is it now impossible to override / overload a class methods EVEN if there is no interface defined...
if there was an interface defined I'd appreciate that any subclass needs to conform to that
interface.. but otherwise the class is just a class and methods should be completely overridable and
overloadable.  There are literally millions of cases where this may be required, and if you
don't think its a bug, then I'll raise another ticket and try get someone else with a
brain to look at this.

------------------------------------------------------------------------
[2015-12-15 08:46:29] chazmead89 at gmail dot com

If the parent class specifies an interface without specifying an interface.. then what is the point
in the Interface type?

------------------------------------------------------------------------
[2015-12-14 19:35:47] requinix@php.net

"Basic method overriding" is you making sure your child class conforms to the interface
specified by the parent class. That includes type hints.

------------------------------------------------------------------------
[2015-12-14 18:28:48] chazmead89 at gmail dot com

Description:
------------
PHP Warning:  Declaration of B::foo(string $boo) should be compatible with A::foo(array $bar) in php
shell code on line 3

This should not be a warning... this is basic method overriding, and should be perfectly good code.

For some reason I get a fatal error on apache handler, but only warning on CLI, possibly Xdebug RC4
escalating it as a bug in XDdebug.

Test script:
---------------
class A {
  public function foo(string $bar) { print_r($bar); }
}

class B extends A {
  public function foo(array $bar) { print_r($bar); }
}

Actual result:
--------------
PHP Warning:  Declaration of B::foo(string $boo) should be compatible with A::foo(array $bar) in php
shell code on line 3

Warning: Declaration of B::foo(string $boo) should be compatible with A::foo(array $bar) in php
shell code on line 3


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=71119&edit=1


Thread (8 messages)

« previous php.bugs (#197901) next »