Bug #68041 [Nab]: Interfaces - Interfaced instance can access undefined methods
| From: | sosingus at gmail dot com | Date: | Thu, 18 Sep 2014 11:34:55 +0000 |
| Subject: | Bug #68041 [Nab]: Interfaces - Interfaced instance can access undefined methods | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187588@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68041&edit=1
ID: 68041
User updated by: sosingus at gmail dot com
Reported by: sosingus at gmail dot com
Summary: Interfaces - Interfaced instance can access
undefined methods
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Linux
PHP Version: 5.4.32
Block user comment: N
Private report: N
New Comment:
Hi johannes,
why do you think it is not a bug?
I'm not saying interfaces should be used this way, but there is a great chance that by mistake
a method that does not belong to interface could be used, and everything will work until another
class implementing same interface (without additional method/s) would be used instead in same code.
Then code would not work any more and there is no way to predict this.
I might say Interface is like a Facade to a class exposing only defined methods, nothing else.
I hope U understand my point of view. Interface needs to be more strict.
Languages that compile code prior being run, would not compile such code at all.
Please at least think of it from developer perspective.
I can't really get into patching php core code, sorry, I don't have time. I just wanted to
notify someone about this as I believe it is a bug/core design issue/ and it would mean a lot to
have at least RuntimeException on wrong interface usage like sample code I provided.
Thanks.
Previous Comments:
------------------------------------------------------------------------
[2014-09-18 11:13:46] johannes@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
.
------------------------------------------------------------------------
[2014-09-18 09:47:33] sosingus at gmail dot com
Description:
------------
If we use interfaced instance to access a method not defined in interface itself, code will work!
Expected is RuntimeException of some kind.
Same applies for using parent class on instance of some subclass. If anyone cares I'll serach
and create similar bug report for classes.
This behavior can lead to serious problems in any serious OO code. Worst of all it cannot be
predicted in any way.
I think this is a system issue and probably exists since the OO beginning in php.
Test script:
---------------
<?php
interface TestInterface
{
public function printMe();
}
class TestClass implements TestInterface
{
public function printMe()
{
print "1\n";
}
public function printMeToo()
{
print "2\n";
}
}
function testInterfaceShouldFail(TestInterface $instance)
{
$instance->printMe();
$instance->printMeToo();
}
testInterfaceShouldFail(new TestClass());
Expected result:
----------------
raised/thrown RuntimeException
Actual result:
--------------
1
2
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68041&edit=1