D
D
DarkUser2016-01-26 18:11:25
PHP
DarkUser, 2016-01-26 18:11:25

An example of using the factory method pattern in php in practice?

Who can bring an example of using the factory method pattern from their application or open source?
An example of use, what problem does it solve? Enough description.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wol_fi, 2016-01-26
@wol_fi

For example

class Parser
{
    public static function factory($url)
    {
        $url = Functions::getRealUrl($url);
        $className = static::getParserClassName($url);
        $instance = new $className($url);
        if (!($instance instanceof Parser_Interface)) {
            throw new Exception("{$className} must implement Parser_Interface");
        }
        return $instance;
    }
}

The factory method creates a parser object depending on the url (specific for some domains, default for others).
Used like this:
$parser = Parser::factory('https://toster.ru/q/286564');
$parser->getTitle();
$parser->getBody();

A
Alexander Aksentiev, 2016-01-26
@Sanasol

https://github.com/zfcampus/zendcon-design-pattern...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question