I have a Trait called TricksTrait with a few string parsing and replacing functions that I use alot (most I read from the net and remove unwanted chars for domPdf || Maatwebsite Excel Html rendering engines) in my laravel Controllers.
I pass reference to the Trait by using use TricksTrait within my class FilmController. The methods are used by referencing $this e.g. $this->str_replace_json('The Fox & Hound').
The thing is I want to use the TricksTrait method str_replace_json within a view that is outputting html in a loop. Using $this->str_replace_json($variable) in my view fails with error Call to undefined method Illuminate\View\Engines\CompilerEngine::str_replace_json() .
So I tried creating a function variable by passing the TricksTrait method to a variable within a controller class function:
class FilmController extends Controller
{
use TricksTrait;
public function downloadFile ($id) {
$str_replace_json = "self::str_replace_json"; // gives error Class 'self' not found
}
}
The Trait function reference fails as Not found for all the below:
"self::str_replace_json"; // gives error Class 'self' not found
"FilmController::str_replace_json"; // gives error Class 'FilmController' not found
"TricksTrait::str_replace_json"; // gives error Class 'TricksTrait' not found
How do I pass a Trait method reference to a variable that has function scope within the Class that inherits the Trait?