php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44043 Enforcing a method without specifying the arguments
Submitted: 2008-02-04 18:44 UTC Modified: 2015-08-07 11:28 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: j dot boggiano at seld dot be Assigned: cmb (profile)
Status: Closed Package: *General Issues
PHP Version: 6CVS-2008-02-04 (snap) OS: *
Private report: No CVE-ID: None
 [2008-02-04 18:44 UTC] j dot boggiano at seld dot be
Description:
------------
The purpose of this is to allow an abstract class or an interface to enforce the implementation of a method, without however enforcing the arguments signature.

This is -as I understand it- not a bug, but a feature request.

Reproduce code:
---------------
/***
 * This works in PHP5 but does not anymore in PHP6
 */
class Parnt {
	public function foo() {}
}
class Child extends Parnt {
	public function foo($arg){}
}

/***
 * Those don't work in either 5 or 6
 */
abstract class Abst {
	abstract public function foo();
}
class Child2 extends Abst {
	public function foo($arg){}
}

interface Int {
	public function foo();
}
class Child3 implements Int {
	public function foo($arg){}
}

Expected result:
----------------
/***
 * Proposal for a solution
 */
abstract class AbstFixed {
	abstract public function foo;
}
class Child4 extends AbstFixed {
	public function foo($arg){}
}

// => Declaring the method without parenthesis (which currently throws a parse error) would allow the implementations of this method to use any argument signature they please.

Actual result:
--------------
At the moment those three examples throw a fatal error in PHP6 with : Declaration of Child::foo() must be compatible with that of Parnt::foo()

I don't think it is a bug, but I think having the capability to allow variable function signatures would definitely be a plus, especially with PHP6 coming that prevents the first example to work.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-04 20:27 UTC] helly@php.net
What we could do is adding '...'. That way you can do stuff like:

interface Foo {
  function bar($first, ...);
}

Now all classes that derive from Foo must implement the same function protocol, for instance:

class Baz {
  function bar($first, ...) {}
}

Anything else would violate the inheritance rules.
 [2008-02-04 20:58 UTC] j dot boggiano at seld dot be
As I understand it, the following example would work, right ?

interface Foo {
	function bar(...);
}
class Baz implements Foo {
	function bar($first, $second, $third, $whatever) {}
}

If so, it's exactly what I'm looking for, your syntax is as good as mine as far as I'm concerned. In fact it's even better as it allows for a partial signature enforcement while still leaving the argument list "opened".
 [2015-08-06 21:35 UTC] cmb@php.net
-Status: Open +Status: Feedback -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: cmb
 [2015-08-06 21:35 UTC] cmb@php.net
Well, there'll be no PHP 6, and PHP 7 behaves basically identical
to PHP 5, see <http://3v4l.org/eamAR>.

Anyhow, I have doubts that this would be a desirable feature,
because an instance of a subclass is supposed to be able to be
used instead of an instance of its superclass (LSP). Consider:

<?php
function do_something(Parnt $parent) {
    $parent->foo();
}
do_something(new Child);
?>

Furthermore, as of PHP 5.6.0 there is the possibility to declare
variadic functions, what's basically what Marcus suggested.

Can this feature request be closed?
 [2015-08-07 10:21 UTC] seld@php.net
Well yes, let's just close this, it's quite clear it is not gonna happen at this point, and variadics kind of serve that purpose. I can't close it though but feel free whoever has access.
 [2015-08-07 11:28 UTC] cmb@php.net
-Status: Feedback +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 22:01:31 2024 UTC