php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41162 Interface methods not implementable using __call
Submitted: 2007-04-22 13:11 UTC Modified: 2007-05-01 23:00 UTC
From: bugs dot php dot net dot nsp at cvogt dot org Assigned:
Status: Closed Package: Class/Object related
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2007-04-22 13:11 UTC] bugs dot php dot net dot nsp at cvogt dot org
Description:
------------
When implementing an interface one cannot use __call to implement methods as this will lead to a FATAL ERROR. However it would be quite convenient if one could do this. It would e.g. allow to generically pass on method calls to an object that is stored in a local variable and implements the interface.

Reproduce code:
---------------
interface MyInterface{
	public function myMethod();
}

class MyImplementation implements MyInterface{
	public function __call( $method, $parameters ){
		// do something
	}
}

Expected result:
----------------
__call takes care of not explicitly implemented methods declared in MyInterface.

Actual result:
--------------
Fatal error:  Class MyImplementation contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (MyInterface::myMethod)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-22 13:21 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You would be missing two completely different things, descriptive and non descriptive semantic elements.

What you can do is providing the implementation of the interface methods all with the same body:

function <name>(<signature>) {
  return $this->__call(array($this, __FUNCTION__), func_get_args());
}

where <name> and <signature> have to be replaced with the method name in question and its parameters respectively.
 [2007-04-22 15:06 UTC] bugs dot php dot net dot nsp at cvogt dot org
Thank you for the quick reply and the proposed generic function body.

What do you mean by "You would be missing two completely different things, descriptive and non descriptive semantic elements."?

I could use

function <name>(<signature>) {
  $args = func_get_args(); return $this->__call(__FUNCTION__, $args);
}

(which fixes 2 bugs in your suggestion) but I would prefer an abstraction over copy&paste.
 [2007-05-01 21:30 UTC] bugs dot php dot net dot nsp at cvogt dot org
Isn't __call a reasonable way to implement methods? Sorry to re-open this one, but I am not convinced of the opposite yet. But I am open for an explanation.
 [2007-05-01 22:18 UTC] helly@php.net
Sorry for the type, it should read "mixing".

Once again. Interfaces are declared stuff - __call is not.

Since PHP does not have templates and will never have them that is the end of the story.

Well you could experiment with pecl/runkit' runkit_method_add().
 [2007-05-01 23:00 UTC] bugs dot php dot net dot nsp at cvogt dot org
I see your point now, thx for your answer.

pecl/runkit "For all those things you.... probably shouldn't have been doing anyway...." ;). Good to keep in mind that such thing exists, might come in handy some time. Thx for the hint.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 22:01:29 2024 UTC