I am writing a program which processes geographical coordinates of a city and I want to randomly select my city in each run; the process for all cities are the same.
I have a PHP class for each city which contains geographical coordinates of it; for example:
<?php
namespace Utility\Locations;
class Tehran
{
const MIN_LAT = 35.325;
const MAX_LAT = 35.390;
const MIN_LNG = 51.165;
const MAX_LNG = 51.230;
}
In another PHP file I make use of the class as:
use Utility\Locations\Tehran;
use Utility\Locations\Karaj;
...
protected function MyProcessingMethod () {
...
$city = Faker::create()->randomElement(array("Tehran", "Karaj"));
echo $city::MIN_LAT;
...
}
...
If it helps, the above mentioned file is a CodeCeption Cest
and MyProcessingMethod
is used as a DataProvider
. When I run my tests using codecept run scenarios/MyCest.php
, I get this error:
PHP Fatal error: Uncaught Error: Class 'Tehran' not found in /home/zeinab/PhpstormProjects/test/scenarios/MyCest.php:190
Stack trace:
#0 [internal function]: MyCest->MyProcessingMethod()
#1 /home/zeinab/PhpstormProjects/test/vendor/codeception/codeception/src/Codeception/Util/ReflectionHelper.php(47): ReflectionMethod->invokeArgs(Object(MyCest), Array)
#2 /home/zeinab/PhpstormProjects/test/vendor/codeception/codeception/src/Codeception/Test/Loader/Cest.php(65): Codeception\Util\ReflectionHelper::invokePrivateMethod(Object(MyCest), Object(ReflectionMethod))
#3 /home/zeinab/PhpstormProjects/test/vendor/codeception/codeception/src/Codeception/Test/Loader.php(109): Codeception\Test\Loader\Cest->loadTests('/home/zeinab/Ph...')
#4 /home/zeinab/PhpstormPro in /home/zeinab/PhpstormProjects/test/scenarios/MyCest.php on line 190
I've read PHP official documentations on this type of usage, but all examples there were using the classes that are defined inside destination file.
I also tried this:
$city = Faker::create()->randomElement(array(Tehran::class, Karaj::class));
echo $city::MIN_LAT;
But I got the same error.
Is there anyway to do what I want to?
EDIT 1:
I have put the path to the classes in psr-4
tag of my composer.json
:
"autoload": {
"psr-4": {
"Utility\\": "Utility/"
}
}
It seems there is no problem in loading the classes, since using them directly works fine. For example the following code is doing great:
echo Tehran::MIN_LAT;