definitivamente desistiendo...
referencia de php: [URL="http://www.php.net/manual/en/language.oop5.interfaces.php"]Object Interfaces[/URL] [URL="http://php.net/manual/en/language.oop5.typehinting.php"]Type Hinting[/URL]
me he puesto a investigar para mi y para los que vengan detras y aquí me he encontrado con estos dos usuarios aclarando lo siguiente:
cyrille.berliat[no spam]free.fr
Código PHP:
Interfaces and Type Hinting can be used but not with Inherintance in the same time :
<?
class AbstractClass
{ public function __ToString ( ) { return 'Here I\'m I'; } }
class DescendantClass extends AbstractClass{}
interface MyI
{
public function Hello ( AbstractClass $obj );
}
class MyClassOne implements MyI
{
public function Hello ( AbstractClass $obj )
{
echo $obj;
}
} // Will work as Interface Satisfied
class MyClassTwo implements MyI
{
public function Hello ( DescendantClass $obj )
{
echo $obj;
}
} // Will output a fatal error because Interfaces don't support Inherintance in TypeHinting
//Fatal error: Declaration of MyClassTwo::hello() must be compatible with that of MyI::hello()
?>
nicholas at nicholaswilliams dot info
Código PHP:
Please note that the following will not work:
<?php
abstract class Object
{
public abstract function toString( );
public abstract function equals( Object &$o );
}
class Chair extends Object
{
public function toString( )
{
return 'This is a chair.';
}
public function equals( Chair &$o )
{
return TRUE;
}
}
class Table extends Object
{
public function toString( )
{
return 'This is a table.';
}
public function equals( Table &$o )
{
return TRUE;
}
}
$chair = new Chair();
$table = new Table();
echo $chair->equals( $table );
?>
The expected output is "Fatal error: Argument 1 passed to Chair::equals() must be an instance of Chair, called in [filename] on line 38 and defined in [filename] on line 16" but instead you get "Fatal error: Declaration of Chair::equals() must be compatible with that of Object::equals() in [filename] on line 20".
This is unlike other OO languages (secifically Java) which not only allow but expect this type of code. It is in the nature of abstraction. However, you can get similar results using the following code instead:
<?php
abstract class Object
{
public abstract function toString( );
public abstract function equals( self &$o );
}
class Chair extends Object
{
public function toString( )
{
return 'This is a chair.';
}
public function equals( self &$o )
{
return TRUE;
}
}
class Table extends Object
{
public function toString( )
{
return 'This is a table.';
}
public function equals( self &$o )
{
return TRUE;
}
}
$chair = new Chair();
$table = new Table();
echo $chair->equals( $table );
?>
This code gives the expected result "Fatal error: Argument 1 passed to Chair::equals() must be an instance of Chair, called in [filename] on line 38 and defined in [filename] on line 16". This is the proper behavior but isn't the most intuitive approach for those of us used to OO programming.
Hope this helps someone :-).
Nicholas
nicholas nos propone un segundo metodo, pero que en este caso no sirve, una pena... PHP ya puede mejorar la OOP si no vuelvo a ASP jejeje, noooo me ha gustado mucho el cambio, lamentablemente de un lenguaje nativamente estructurado no me puedo esperar mas, en definitiva, si a la coleccion de anillos le pasan un collar o una pulsera pues los almacenara.... asi que ni coleccion de anillos ni coleccion de coches, ni de nada, coleccion a secas y ya esta..
saludos amigos
PD: aun tengo esperanzas de que alguien venga con el metodo... o habra que esperar a PHP 5.4 y la mejora del Type Hinting