Answer the question
In order to leave comments, you need to log in
How to create a class with a factory method?
In Laravel I have the following classes:
<?php namespace App\Models;
use Illuminate\Support\Str;
interface IParser {
public function get();
}
class ParserFactory {
public static function create($engine)
{
$class = Str::title($engine) . 'Parser';
return new $class;
}
}
class FirstParser implements IParser {
public function get()
{
echo 1;
}
}
$engine = 'first';
$parser = ParserFactory::create($engine);
FatalErrorException in Parser.php line 14: Class 'FirstParser' not found
public static function create($engine)
{
$class = new FirstParser();
return $class;
}
Answer the question
In order to leave comments, you need to log in
You need to specify the full path to the class, along with the namespace;
$class = 'App\\Models\\' . Str::title($engine) . 'Parser';
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question