Import php class from file

Carol.Kar

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?

pr1nc3
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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to import from a file a class that will later be defined

From Dev

Is it possible to import a class from Materialize into an SCSS file?

From Dev

How to import a Class from another py file?

From Dev

Typescript: how to import a class from a javascript file?

From Dev

Cannot import class from other file

From Java

Scripting java : import a class from an external file

From Dev

Cant use a class from import file

From Dev

Cannot import an exported class from a typescript file

From Dev

How to import data from file into class object?

From Dev

dynamically import class from file and use a function in it

From Dev

'from file import class' tries to import other classes as well

From Dev

How to import class from `js` file inside `html` file?

From Dev

How to import a python class from a file in a different folder?

From Dev

import code from javascript file inside typescript class

From Dev

how to import variable from a different file class function

From Dev

Using Jenkins Shared Libraries, import Class from file within vars/?

From Dev

Does importing a class from another file also import that files dependencies?

From Java

Error while using the load class from Java to import property file

From Dev

How to import a class from a different source file in D?

From Java

How to import python class file from same directory?

From Dev

Android studio, import class disappear from gradle file

From Dev

How to import one class from many classes on one python file

From Dev

Python Import class in __init__.py from file in same package

From Dev

Why import class from another file will call __init__ function?

From Dev

PowerShell Class Inheritance from separate file using Import-Module

From Dev

How to import a class into a python file from an outside directory?

From Dev

How to change a variable from a class and be able to import it to another file?

From Dev

How do I import a class from a file in Scala?

From Dev

Trying to import a class from another file but my test breaks

Related Related

  1. 1

    How to import from a file a class that will later be defined

  2. 2

    Is it possible to import a class from Materialize into an SCSS file?

  3. 3

    How to import a Class from another py file?

  4. 4

    Typescript: how to import a class from a javascript file?

  5. 5

    Cannot import class from other file

  6. 6

    Scripting java : import a class from an external file

  7. 7

    Cant use a class from import file

  8. 8

    Cannot import an exported class from a typescript file

  9. 9

    How to import data from file into class object?

  10. 10

    dynamically import class from file and use a function in it

  11. 11

    'from file import class' tries to import other classes as well

  12. 12

    How to import class from `js` file inside `html` file?

  13. 13

    How to import a python class from a file in a different folder?

  14. 14

    import code from javascript file inside typescript class

  15. 15

    how to import variable from a different file class function

  16. 16

    Using Jenkins Shared Libraries, import Class from file within vars/?

  17. 17

    Does importing a class from another file also import that files dependencies?

  18. 18

    Error while using the load class from Java to import property file

  19. 19

    How to import a class from a different source file in D?

  20. 20

    How to import python class file from same directory?

  21. 21

    Android studio, import class disappear from gradle file

  22. 22

    How to import one class from many classes on one python file

  23. 23

    Python Import class in __init__.py from file in same package

  24. 24

    Why import class from another file will call __init__ function?

  25. 25

    PowerShell Class Inheritance from separate file using Import-Module

  26. 26

    How to import a class into a python file from an outside directory?

  27. 27

    How to change a variable from a class and be able to import it to another file?

  28. 28

    How do I import a class from a file in Scala?

  29. 29

    Trying to import a class from another file but my test breaks

HotTag

Archive