I'm currently evaluating if I'm gonna use traits in my software project. In this project I want to use aspect oriented programming (https://en.wikipedia.org/wiki/Aspect-oriented_software_development) to decouple system logic from business logic.
I want to have a logger trait and a cacheing trait. Now the question: Is it possible to use both traits on the same class method? With a defined order of execution?
Thought Example:
trait Logger {
public function warn($argument) {
$result = parent::__FUNCTION__($argument);
if(!$result) echo "warn";
return $result;
}
}
trait Cacher {
public function cache($argument) {
if(CACHE::contains(__FUNCTION__, $argument)) {
return CACHE::get(__FUNCTION__, $argument);
}
return parent::__FUNCTION__($argument);
}
}
class MyBusiness {
use Logger, Cache { Logger::logger as myTest, Cache::cache as myTest before Logger }
public function myTest(boolean $someBool) {
return !$someBool;
}
}
Order of execution should be Enter Cache::cache, Enter Logger::logger, Enter myTest return !bool, log it, cache it.