Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dnaber-de opened this issue Nov 8, 2016 · 1 comment
Assignees
Labels

Comments

@dnaber-de
Copy link
Member

dnaber-de commented Nov 8, 2016

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.

@dnaber-de dnaber-de added the bug label Nov 8, 2016
@dnaber-de dnaber-de self-assigned this Nov 8, 2016
dnaber-de added a commit that referenced this issue Nov 8, 2016
@dnaber-de
Copy link
Member Author

I think that can be closed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant