Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13304

Strange but true! Working with interfaces in PHP

Newsgroups comp.lang.php
Date 2014-03-31 15:31 -0700
Message-ID <bdfad044-74c3-4256-9ae2-2b6035d87ecc@googlegroups.com> (permalink)
Subject Strange but true! Working with interfaces in PHP
From Mirco <mirco.campanella@gmail.com>

Show all headers | View raw


I was working on a software of arbitrary complexity when I stumbled upon this strange behavior of the PHP compiler.

Problem:
I have these interfaces and classes:

interface IBaseA {
	
}

interface IA extends IBaseA {

}

interface IBaseB {
	
	public function match( IBaseA $subject );

}

interface IB extends IBaseB{
}

class A implements IA{

}

class B implements IB{
	public function match( IA $subject ){
		echo "Am I working? o_O<hr>";
	}
}

$a = new A();
$b = new B();
$b->match($a);

Running the above code, this is what I get: 
Fatal error: Declaration of B::match() must be compatible with that of IBaseB::match()

This behavior is very odd, because A implements IA that extends from IBaseA. So IA is an interface of type IBaseA. Methods I will put in IBaseA will be also in IA.
Class B accepts, in the match() method, an instance of IA.
Class B extends from IBaseB that in turn accepts an instance of IBaseA.

B should accept IA. In fact the interface that B implements accepts a more specific version of IA, but the Fatal Error still pops up.

The things become more strange if we look at another example.
This one:

interface IBaseA {
	
}

interface IA extends IBaseA {

}

interface IBaseB {
	
	public function match( IA $subject );

}

interface IB extends IBaseB{
}

class A implements IBaseA{

}

class B implements IB{
	public function match( IA $subject ){
		echo "Am I working? o_O<hr>";
	}
}

$a = new A();
$b = new B();
$b->match($a);

Here, what I get it's not a Fatal Error, but a Catchable Fatal Error.
This is an error that could be catched by this simple function: 

function myErrorHandler($errno, $errstr, $errfile, $errline) {
  if ( E_RECOVERABLE_ERROR===$errno ) {
    echo "'catched' catchable fatal error<br>";
    return true;
  }
  return false;
}
set_error_handler('myErrorHandler');

This time though, the compiler should not let me continue with the execution because:

B implements IB that extends from IBaseB.
IBaseB accepts instances of IA.
IA extends from IBaseA.
A implements IBaseA.

What i'm passing to the match() method of B is an instance of A, that is more strict that IA.
The compiler should throw a Fatal Error and it shouldn't let me continue with the execution! The inner details of B's match() implementation require an instance of IA with its own specific methods that could not be provided by IA's father, IBaseA.

Would you please shed a light onto this one?
What do you think about it?

Back to comp.lang.php | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Strange but true! Working with interfaces in PHP Mirco <mirco.campanella@gmail.com> - 2014-03-31 15:31 -0700
  Re: Strange but true! Working with interfaces in PHP Jerry Stuckle <jstucklex@attglobal.net> - 2014-04-01 12:43 -0400
  Re: Strange but true! Working with interfaces in PHP Christoph Michael Becker <cmbecker69@arcor.de> - 2014-04-03 13:20 +0200
    OT: "overlong lines"   Re: Strange but true! Working with interfaces in PHP Luuk <luuk@invalid.lan> - 2014-04-05 10:50 +0200
      Re: OT: "overlong lines"   Re: Strange but true! Working with interfaces in PHP Tim Streater <timstreater@greenbee.net> - 2014-04-05 10:20 +0000
        Re: OT: "overlong lines"   Re: Strange but true! Working with interfaces in PHP Luuk <luuk@invalid.lan> - 2014-04-05 11:28 +0200
        "overlong lines" (was: OT: "overlong lines"   Re: Strange but true! Working with interfaces in PHP) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-04-05 14:21 +0200
          Re: "overlong lines" Luuk <luuk@invalid.lan> - 2014-04-05 15:00 +0200
            Re: "overlong lines" Christoph Michael Becker <cmbecker69@arcor.de> - 2014-04-05 15:28 +0200
  Re: Strange but true! Working with interfaces in PHP Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2014-04-03 07:19 -0700

csiph-web