When I run a simple project with Phalcon, It is Ok. But I'm going to run another Phalcon project that wrote by another person but I get an error: Fatal error: Declaration of Core\Config::toArray() must be compatible with Phalcon\Config::toArray(): array in C:\xampp\htdocs\phalcon\phalcon\apps\Core\Config.php
I searched a lot but could not solve this problem.
Core\Config code:
class Config extends \Phalcon\Config
{
public function toArray()
{
$configArray = parent::toArray();
unset($configArray['index']);
return $configArray;
}
}
Phalcon\Config Code:
class Loader
{
/**
* Load config from file extension dynamical
*
* @param string $filePath
*
* @return Config
* @throws Exception
*/
public static function load($filePath)
{
if (!is_file($filePath)) {
throw new Exception('Config file not found');
}
$extension = strtolower(
pathinfo(
$filePath,
PATHINFO_EXTENSION
)
);
switch ($extension) {
case 'ini':
return new Ini($filePath);
case 'json':
return new Json($filePath);
case 'php':
case 'php5':
case 'inc':
return new Php($filePath);
case 'yml':
case 'yaml':
return new Yaml($filePath);
default:
throw new Exception(
'Config adapter for .' . $extension . ' files is not support'
);
}
}
}
Your issue is only partially related to Phalcon. The error itself is a sub error of PHP directly. https://3v4l.org/F8EfM
class Something {
public function toArray(): array
{
return [];
}
}
class SomethingElse extends Something {
public function toArray()
{
return [];
}
}
Effectively, the parent class defines a return type hint, but the child does not. These are considered incompatible. In order to fix it, the child class must have the same declaration as the parent. https://3v4l.org/b3BGR
class SomethingElse extends Something {
public function toArray(): array
{
return [];
}
}
Now, that said, I did mention that it only partially
applies to Phalcon. The core error is PHP, but the underlying cause is your version of Phalcon compared to the codebase. That is to say that the project that is working either has no such issue, or that it's using the correct version of Phalcon in relation to the code. The project that is failing was likely built on an older version of Phalcon than you have installed. Using Return Type Hints is a newer paradigm. In order to 'fix' the issue, you have two options. The first is to upgrade the code to be compatible with the newer version of Phalcon (By simply editing Core\Config::toArray
to have : array
). However, this can quickly become a project in it's own right owing to having to fix all such issues. The second option is to downgrade the version of Phalcon you have installed to match the version that it was originally built on. Unfortunately, I have no way of determining the version it was originally built on. It's also possible you may have to downgrade your PHP version, but I'm not positive.
Side Note: This scenario is one of the reasons why I personally no longer use XAMPP and instead use Docker Images through Rancher Desktop
. In this way, I can explicitly set the version of Phalcon & PHP directly in the image.