0

I am using PHP 7.3.5 and I have two files, class Foo(), which is in the directory ./db/models and my main.php, which is in the root of the project.

<?php

use Goutte\Client;

class Foo
{

    function getHello()
    {
       echo "Hello world!<br>";
    }


}

My main.php file looks the following:

<?php

include("./db/models/Foo.php");

$foo = new Foo;

$foo->getHello();

However, I get the following error:

PHP Fatal error: Uncaught Error: Class 'Foo' not found in C:\Users\Desktop\Code\test-project\main.php:5

Any suggestions what I am doing wrong?

6
  • does the include throw any warnings? Replace with require and see if you fatal error Commented May 17, 2019 at 14:15
  • I don't see why this would fail. Is that the exact code you have? There's no namespace declaration in Foo.php-file? Commented May 17, 2019 at 14:20
  • 1
    @MagnusEriksson ^^ pos 404 error (under the hood) on that include is my hunch Commented May 17, 2019 at 14:20
  • 1
    When including files, it's a good idea to use __DIR__ to get the absolute path to the file you're in. If just do include 'db/...'; it will load the file relative from the top most file (usually the index.php file). Check out this answer that explains it in more detail. Commented May 17, 2019 at 14:28
  • 2
    I would also recommend making sure that you're using error_reporting(E_ALL); while you develop so you make sure you see all warnings as well. The error reporting level can also be set in your php.ini-file. Commented May 17, 2019 at 14:31

2 Answers 2

2
include("db/models/Foo.php");

Since your db folder is inside route you can simple access it like that.

You don't need the ./ to move one step before cause your main is already in the root. You need to move inside your nested folders to reach the Foo.php

Since you did not provide the full tree i assumed that your path to Foo.php is:

C:\Users\Desktop\Code\test-project\db\models\Foo.php
Sign up to request clarification or add additional context in comments.

2 Comments

Thx. How can I then load the class?
Like you did. Your code should work from there. just use : $foo = new Foo(); and you are good to go
0

./db is your hidden directory ?? If yes this can cause issue to loading files. Please verify directory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.