Alix Ohrt
Alix Ohrt

Reputation: 199

Laravel Class Namespacing

I have created a few custom classes in App/ of laravel 5 as such:

App/Database.php
App/Table.php
App/ContactsTable.php

In my ContactsTable class, I'm extending the Table class

use Table;
/**
 * Description of ContactsTable
 * 
 * @author john
 */
class ContactsTable extends Table {

I have the aliases setup in config/app.php for Table (App\Table) and ContactsTable (App\ContactsTable). But when loading the page it gives me an error:

"The use statement with non-compound name 'Table' has no effect"

I have tried multiple combinations of importing the class in different ways, with no luck.

Any advice?

Upvotes: 1

Views: 116

Answers (1)

user1669496
user1669496

Reputation: 33148

It's because you are currently in the same namespace as the Table class. Since you also aliased it as Table, it probably doesn't know what Table you are talking about, the aliased one which is Table or the Table that currently resides in your current namespace, which both happen to also be the same thing.

Remove the alias and remove the use statement. Since these classes are in the same namespace, there is no need to do either of those things.

It's also likely a very good idea to keep both un-aliased and in the App namespace because those are both pretty common names and might end up causing you more trouble in the future.

I just tried to recreate this and everything works fine here. Just to be sure though, here is the code I used...

app\ContactsTable.php

namespace App;

class ContactsTable extends Table {

}

app\Table.php

namespace App;

class Table {

}

routes.php

Route::get('test', function()
{
    $table = new \App\ContactsTable();

    echo get_class($table);
});

And going to myapp.local/test, it outputs App\ContactsTable

Upvotes: 1

Related Questions