php require class (undefined or fatal error)

I am fairly new to PHP OOP and I can't seem to get my class files to require correctly. Whenever I attempt to require them I get a fatal error. Furthermore, phpstorm says my classes are undefined. The Path to location shows up as valid as phpstorm isn't saying path not found. But when run the classes can't be found.

ToyPlansV1.0/TestMain.php --> This class calls setup on the Test class ToyPlansV1.0/includes/DatabaseHandler.php -->Underlying class to be tested ToyPlansV1.0/includes/TestClasses/DBHandlerTest.php -->This class is the Test class

Contents of TestMain.php

<?php
    require ("includes/TestClasses/DBHandlerTest.php");
    $DBTest = new DBHandlerTest();
    $DBTest.Setup();
?>

Contents of DBHandlerTest.php

<?php
    namespace includes\TestClasses;
    require("../DatabaseHandler.php");
    class DBHandlerTest {
        protected $DB;
        protected function Setup(){
        $this->$DB = new DatabaseHandler();
    }
} 

The errors I get when running TestMain.php are:

require(../DatabaseHandler.php): failed to open stream: No such file or directory in C:\wamp\www\ToyPlansV1.0\includes\TestClasses\DBHandlerTest.php on line 3

Fatal error: require(): Failed opening required '../DatabaseHandler.php' (include_path='.;C:\php\pear') in C:\wamp\www\ToyPlansV1.0\includes\TestClasses\DBHandlerTest.php on line 3

PHP Fatal error: require(): Failed opening required '../DatabaseHandler.php' (include_path='.;C:\php\pear') in C:\wamp\www\ToyPlansV1.0\includes\TestClasses\DBHandlerTest.php on line 3

Any ideas where I am going wrong / how to fix the require paths?

EDIT updated code, nearly working.

<?php
//require ("includes/TestClasses/DBHandlerTest.php");
require(__DIR__.'/includes/TestClasses/DBHandlerTest.php');
use includes\TestClasses\DBHandlerTest As DBHandlerTest;
$DBTest = new DBHandlerTest();
$DBTest.Setup();
?>

<?php
namespace includes\TestClasses;
require(__DIR__.'/../DatabaseHandler.php');
use includes\DatabaseHandler As DatabaseHandler;
class DBHandlerTest
{
    protected $DB;
    public function Setup()
    {
        $this->$DB = new DatabaseHandler();
    }
}

Answers


In TestMain.php you have to declare your DBHandlerTest class with use like this:

require (__DIR__.'/includes/TestClasses/DBHandlerTest.php');

use includes\TestClasses\DBHandlerTest As DBHandlerTest;

$DBTest = new DBHandlerTest();
$DBTest->Setup();

And in DBHandlerTest.php, you have to make your Setup() method public:

namespace includes\TestClasses;

require(__DIR__.'/../DatabaseHandler.php');

class DBHandlerTest
{
    protected $DB;

    public function Setup()
    {
        $this->$DB = new DatabaseHandler();
    }
}

This will work, but apart from that you should use spl_autoload_register. It will make your life easier.


Need Your Help

Why is this MySQL query slowing down my code

php mysql query-optimization

Currently I have a MySQL query I run in PHP and when going through the results I update the original table but a simple table with 500 rows takes 30 seconds to complete: