When attempting to use a class that exhibits a trait with an abstract method declared AND the abstract method is declared with a parameter that is of type reference to array
I am getting the following error:
PHP Fatal error: MyTrait and MyClass define the same property ($foo) in the composition of MyClass. However, the definition differs and is considered incompatible. Class was composed in MyClass
How to declare an abstract method in a trait in such a way as to require the exhibiting class to only accept an array reference as input parameter?
What is the appropriate syntax for the parameter to the array reference in both the Trait and the exhibiting class?
(updated) example:
<?php
const LOCATION_PRECISION = 7;
const LOCATION_LAT = '40.7591523';
const LOCATION_LNG = '-73.9777136';
trait Geocodes
{
protected $recast = [];
protected $precision = LOCATION_PRECISION;
abstract function reCast(array &$payload);
}
class Location
{
use Geocodes;
//FATAL: can't override here!
//protected $recast = [
// 'lat' => ['index' => 'lat', 'type' => 'double'],
// 'lng' => ['index' => 'lng', 'type' => 'double']
//];
protected $lat = LOCATION_LAT;
protected $lng = LOCATION_LNG;
public function __construct()
{
$this->precision = LOCATION_PRECISION;
$this->recast['lat'] = ['index' => 'lat', 'type' => 'double'];
$this->recast['lng'] = ['index' => 'lng', 'type' => 'double'];
}
public function recast(array &$payload)
{
foreach(array_keys($payload) as $key)
{
var_dump($payload);
$api_key = $this->recast[$key]['index'];
$api_type = $this->recast[$key]['type'];
if(! array_key_exists($api_key, $payload))
$payload[$api_key] = bcadd($payload[$key],0,$this->precision);
}
}
public function getLat() { return $this->lat; }
public function getLng() { return $this->lng; }
}
$loc = new Location();
$payload = ['lat' => $loc->getLat(), 'lng' => $loc->getLng()];
$loc->recast($payload);
echo PHP_EOL.print_r($payload, 1).PHP_EOL;