php-unit-conversion icon indicating copy to clipboard operation
php-unit-conversion copied to clipboard

Using UnitMap to add a unit HeadCount can't find unit

Open slavensaka opened this issue 5 years ago • 1 comments

Hi im having a hard time figuring out why i get a not found. I'm trying to add a Headcount.php unit within Amount unit type. But i get this Class 'PhpUnitConversion\Unit\Amount\HeadCount' not found

I'm using the UnitMap::add

UnitMap::add(
dirname(__DIR__, 2) .'/helpers/unitconversion/Fixtures/Amount/HeadCount.php',
 "PhpUnitConversion\\Unit",
\PhpUnitConversion\Unit\Amount::class
);

First parameter is the server path that's the correct path to my HeadCount.php Second im not sure what to use Third the class of the unit type I want to add it to

This is my HeadCount.php unit

 namespace PhpUnitConversion\Unit\Amount;
 use PhpUnitConversion\Unit\Amount;
 class HeadCount extends Amount
 {
   const FACTOR = 1;
  const SYMBOL = 'hc';
  const LABEL = 'head count';
}

Initlization:

$hd = new  \PhpUnitConversion\Unit\Amount\HeadCount(343);

I need to initilize it like above reason i have dynamic way of calling it

$convertFromClass = "\PhpUnitConversion\Unit\\". $type . "\\" .  $from;
$convertFrom = new $convertFromClass($formattedNumber);

Looking in Map.php source code the static load() adds it to self:$paths like this

"[["/var/www/vhost/biome/vendor/php-unit-conversion/php-unit-conversion/src/Unit/" "PhpUnitConversion\Unit" true null]

["/var/www/vhost/biome/app/helpers/unitconversion/Fixtures/Amount/HeadCount.php" "PhpUnitConversion\Unit" true "PhpUnitConversion\Unit\Amount"]]"

slavensaka avatar Feb 13 '20 15:02 slavensaka

I dont think its possible to re-use the same namespace? It shouldnt be necessary at least.

Namespaces are linked to a folder on disk, the PhpUnitConversion\Unit\Amount namespace should be pointing to a folder in vendor but your HeadCount file isnt located in vendor right? Did you try using a custom namespace?

pimlie avatar Feb 23 '20 12:02 pimlie