I am trying to run different classes dynamically in PHP using some hopefully reusable code, but I am having trouble finding the proper way to do it.
Currently, I am running a POST request with the data flowing as follows:
Route::post('/user/create', 'UserController@createItem');
To UserController:
namespace App\Http\Controllers;
use Illuminate\Cache\Repository;
use App\Models\User;
class UserController extends AbstractController{
const className = 'User';
}
And AbstractController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Exception\RequestException;
use App\Models\User;
class AbstractController extends Controller
{
public function createItem() {
$inputJSON = file_get_contents('php://input');
$this->className::buildItem($inputJSON);
return;
}
}
Which should ideally call a static method called buildItem()
When I test this by hard coding User
into the AbstractController, it works as expected, but when I try to do it this way, I get an error in PostMan:
ErrorException: Undefined property: App\Http\Controllers\UserController::$className in file .../app/Http/Controllers/AbstractController.php on line 14
I have tried declaring and referencing className
a few different ways, but can't seem to get this to work. Any help is appreciated!