- Edited
trait fileLogger{
public function logmessage($message,$level='DEBUG')
{
echo "write $message to a log $level";
echo "<br/>";
}
}
trait sysLogger {
public function logmessage($message,$level='DEBUG')
{
echo "write $message to a log $level";
echo "<br/>";
}
}
class fileStorage
{
use fileLogger,sysLogger
{
fileLogger::logmessage insteadof sysLogger;
sysLogger::logmessage as private logsysmessage;
}
function store($data){
//...
$this->logmessage($data);
$this->logsysmessage($data);
}
}
I don't know how to call the function both they are equal: function logmessage but one is fileLogger and the other is sysLogger
$obj = new fileStorage();
$obj->fileLogger::logmessage(fileLogger);
//phpstorm said uniform variable syntax is only allowed since php 7.0
Warning: Undefined property: fileStorage::$fileLogger in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/using-traits-pag-175/using-traits.php on line 32
Fatal error: Uncaught Error: Class name must be a valid object or a string in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/using-traits-pag-175/using-traits.php:32 Stack trace: #0 {main} thrown in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/using-traits-pag-175/using-traits.php on line 32
$obj->sysLogger::logmessage (sysLogger);
$obj->store("goodbye");
I have this version PHP Version 8.1.16