Skip to content

Composition of properities in trait and class might be incompatible in PHP < 7 #4

Closed
@dnaber-de

Description

@dnaber-de

I discoverd a weird bug on a PHP 5.6 system, when I ran Wonolog within a complete site stack using wp cli:

$ wp site list
PHP Fatal error:  Cannot instantiate abstract class Inpsyde\Wonolog\Data\Log in /.../vendor/inpsyde/wonolog/src/PhpErrorController.php on line 73

It turned out that the error that is actually triggering Wonolog's error handler has the following message:

Inpsyde\Wonolog\Data\Log and Inpsyde\Wonolog\Data\LogDataTrait define the same property ($message) in the composition of Inpsyde\Wonolog\Data\Log. This might be incompatible, to improve maintainability consider using accessor methods in traits instead. Class was composed

This E_STRICT error causes that PHP handles the Log class as abstract one, obviously.

However, here's how you can reproduce it (in PHP 5.6):

<?php # -*- coding: utf-8 -*-

namespace Inpsyde\Wonolog;

use Inpsyde\Wonolog\Data\Log;

set_error_handler( function( ...$args ) {
	//echo $args[1];
	var_dump( ( new \ReflectionClass( \Inpsyde\Wonolog\Data\Log::class ) )->isAbstract() );

	new Log();
	return FALSE;
}, E_ALL );

error_reporting( E_ALL );

$require = function() {
	require_once __DIR__ . '/vendor/psr/log/Psr/Log/LoggerInterface.php';
	require_once __DIR__ . '/vendor/monolog/monolog/src/Monolog/Logger.php';
	require_once __DIR__ . '/vendor/inpsyde/wonolog/src/Data/LogDataInterface.php';
	require_once __DIR__ . '/vendor/inpsyde/wonolog/src/Data/LogDataTrait.php';
	require_once __DIR__ . '/vendor/inpsyde/wonolog/src/Data/Log.php';
	require_once __DIR__ . '/vendor/inpsyde/wonolog/src/Channels.php';
};
$require();

Running this script via CLI will give you this error:

$ php testfile.php
bool(true)
PHP Fatal error:  Cannot instantiate abstract class Inpsyde\Wonolog\Data\Log in /.../testfile.php on line 11

The solution is to remove any duplicate declaration of properties in classes, that was declared by any used trait.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions