php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68041 Interfaces - Interfaced instance can access undefined methods
Submitted: 2014-09-18 09:47 UTC Modified: 2014-09-18 11:34 UTC
From: sosingus at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.32 OS: Linux
Private report: No CVE-ID: None
 [2014-09-18 09:47 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-09-18 11:13 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2014-09-18 11:13 UTC] 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 11:34 UTC] sosingus at gmail dot com
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.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 05:00:01 2026 UTC