Home > Enterprise >  PHP - Undefined type "Customer"
PHP - Undefined type "Customer"

Time:11-12

File structure:

  • Controllers/Foo.php
  • Models/Bar.php
  • Models/Functions.php

In Foo.php, using a require_once() I can use functions from Functions.php, but I cannot instantiate the Bar class? It gives the error "Undefined type 'App\Controllers\Bar'.". I am unsure what I am doing wrong.

If it helps, this is in a Codeigniter project, hence why Bar extends Database.

Foo.php

require (realpath(dirname(__FILE__)) . '\..\Models\Functions.php');
require (realpath(dirname(__FILE__)) . '\..\Models\Bar.php');

class Foo extends Controller {
    public function get_data($input) {
        $oBar = new Bar() // this is where the error occurs
    }
}

Bar.php

<?php

class Bar extends Database {
    public function do_something() {
        echo 'something else';
    }
}

Functions.php

<?php

function say_hi() {
    echo 'hi';
}

CodePudding user response:

Maybe you are using namespaces because the error says it 'App\Controllers\Bar'.

So if Bar is not in a namespace, then use new \Bar() to get it work.

Maybe you have to do new \Models\Bar() or new \App\Models\Bar() but i can't say for real, because related code is missing in your example.

  • Related