elcuy's avatar

T_PAAMAYIM_NEKUDOTAYIM error. Help!

Hello everyone! I'm a Laravel beginner trying to build a REST API for my project. The thing is that I want to inherit all API Controllers from one class so I don't repeat some common functions. Here's my code:

For the APIController.php class:


namespace App\Http\Controllers\API;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class APIController extends Controller
{

    protected $model = null; 

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return $this->model::all();
    }
.
.
.

This is a child class that inherits from APIController


namespace App\Http\Controllers\API;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends APIController
{
    
    public function __construct()
    {
        $this->model = App\User;
    }

}
.
.
.

What i'm trying to do is call UserController from the routes.php file. When UserController gets constructed I'm trying to set the User class on the model attribute of the class. Then I call the methods from UserController defined on APIController

The thing is i'm getting that infamous T_PAAMAYIM_NEKUDOTAYIM error on APIController, in the line that says "return $this->model::all();"

What's the correct way of doing this? Thanks all in advance :)

0 likes
5 replies
bjones2015's avatar
Level 11

Sorry I'm new enough to php that I haven't heard of that infamous error, guess I'll need to make a Google trip after this.

Anyway I think the issue may be with App\User. I don't think you can call it like that. I'd suggest injecting it into the controller or if your project is going to be large enough you could go the repository route. Anyway try this:

namespace App\Http\Controllers\API;

use App\User;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends APIController
{
    
    public function __construct(User $user)
    {
        $this->model = $user;
    }

}
toniperic's avatar

T PAAMAYIM NEKUDOTAYIM actually means "the double colon" in Hebrew.

In PHP, T_PAAMAYIM_NEKUDOTAYIM is the token name for ::, the static separator.

In your UserController you should either use dependency injection (as stated by @bjones2015), or do this

public function __construct()
{
    $this->model = new App\User;
}

Note the new keyword that's missing in your own code.

Hope I could help.

elcuy's avatar

And that's what happens when you code for 14 hours straight... You forget the "new" keyword and stuff, hehe. Thanks @bjones2015 for the injection approach and @toniperic for pointing out the code error. I wish I could accept both answers, but I think i'm going the injection way. Have a great day all!

sharad9838's avatar

T_PAAMAYIM_NEKUDOTAYIM is scope resolution operator used as :: (double colon) .Basically, it used to calling static methods/variables of a Class.

Example usage:- $Cache::getConfig($key);

Please or to participate in this conversation.