<?php
trait implementedBar {
public function bar($a){
echo "a";
}
}
trait abstractBar {
abstract protected function bar($a);
}
abstract class parentImplemented {
use implementedBar;
}
class childAbstract extends parentImplemented {
use abstractBar;
}
//what would happen if you copy/pasted the traits into their classes
//the below classes would definitely cause an error
/**
abstract class parentImplemented {
public function bar($a){
echo "a";
}
}
class childAbstract extends parentImplemented {
abstract protected function bar($a);
}
**/
//below will just work, but we can't really see that implementedBar::bar is an implementation of abstractBar::bar
//instead of it being a replacement, or abstractBar::bar just getting ignored
echo "Example 1:".PHP_EOL;
$b = new childAbstract;
$b->bar([]);
////////////////////////////////////
trait implementedBarTH {
public function bar(array $a){
echo "a";
}
}
trait abstractBarTH {
abstract protected function bar(int $a);
}
abstract class parentImplementedTH {
use implementedBarTH;
}
class childAbstractTH extends parentImplementedTH {
use abstractBarTH;
}
//again, a version with the traits copy/pasted into their classes
//definitely will cause an error
/**
abstract class parentImplementedTH {
public function bar(array $a){
echo "a";
}
}
class childAbstractTH extends parentImplementedTH {
abstract protected function bar(int $a);
}
**/
//This will give us an error related to mismatched type hints.
//This shows that the method in the trait used in the parent class is definitely
//acting as an implementation of the abstract class from the trait used in the child class
$b = new childAbstractTH;
$b->bar([]);
/*** You'll need to comment out 53-59, 80-81 in order to produce an error for the items below ***/
//////////////////////////////////////////////////////////////
class childBothTH {
use abstractBarTH, implementedBarTH;
}
//a version with the traits copy/pasted into the class
//definitely will cause an error
/**
class childBothTH {
abstract protected function bar(int $a);
public function bar(array $a){
echo "a";
}
}
**/
//This will give us an error related to mismatched type hints.
//This shows that the method in the trait that was implemented is definitely
//acting as an implementation of the abstract class from the other trait
$b = new childBothTH;
$b->bar([]);
Example 1:
a
Fatal error: Declaration of parentImplementedTH::bar(array $a) must be compatible with abstractBarTH::bar(int $a) in /in/OKoTY on line 44
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Example 1:
a
Fatal error: Declaration of parentImplementedTH::bar(array $a) must be compatible with abstractBarTH::bar(int $a) in /in/OKoTY on line 44
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Example 1:
a
Fatal error: Declaration of parentImplementedTH::bar(array $a) must be compatible with abstractBarTH::bar(int $a) in /in/OKoTY on line 57
Process exited with code 255.
Output for 7.1.0 - 7.1.33
Example 1:
a
Fatal error: Declaration of abstractBarTH::bar(int $a) must be compatible with parentImplementedTH::bar(array $a) in /in/OKoTY on line 57
Process exited with code 255.
Output for 5.6.38
Example 1:
a
Fatal error: Declaration of abstractBarTH::bar(int $a) must be compatible with parentImplementedTH::bar(array $a) in /in/OKoTY on line 59
Process exited with code 255.