1

I have 2 PHP files as part of a website. The first one has the API functions (pre made ready to use requests), and the other one has the code itself that will call the functions from the first one.

The API file looks like:

<?php
class API_REQUESTS {
    public static function GetSomeData() {
        return some_request('some_url');
    }
}
?>

And the other file — where I want to use the API functions — looks like:

<?php
include '..\path\to\api\api.php';

echo API_REQUESTS.GetSomeData();
?>

I am using XAMPP and it should be setup correctly sense everything works fine except for the php file it self

However, when running the code, I get the following error:

Fatal error: Uncaught Error: Undefined constant "API_REQUESTS" in "path\to\index.php":3 Stack trace: #0 {main} thrown in "path\to\index.php" on line 3

I thought that I might have made a mistake in the include path but when I tried to change it, it gives me a new Warning: include 'wrong\path' Failed to open stream: No such file or directory, so the path is right.

I also tried using require and require_once but I still encounter the Fatal error.

2
  • 2
    API_REQUESTS.GetSomeData() is not proper PHP syntax. You want API_REQUESTS::GetSomeData() for a static call. Also, you might want to read about PSR-4 and use an accepted convention like naming your class ApiRequests instead of API_REQUESTS. Commented Aug 29, 2023 at 18:20
  • yes Thank you, just discovered that and posted an answer Commented Aug 29, 2023 at 18:23

1 Answer 1

0

its been a long time sense i used PHP so i forgot that in PHP i should use the :: operator instead of . to access a class's static members

so the code should look like this :

<?php
include '..\path\to\api\api.php';

echo API_REQUESTS::GetSomeData();
?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.