7
I was taking a look at Manual for PHP and saw an example of implementing the standard Factory
.
Example:
class Exemplo
{
// Método Factory parametrizado
public static function factory($type)
{
if (include_once 'Drivers/' . $type . '.php') {
$classname = 'Driver_' . $type;
return new $classname;
} else {
throw new Exception ('Driver não encontrado');
}
}
}
Exit:
// Carregar um driver MySQL
$mysql = Exemplo::factory('MySQL');
// Carregar um driver SQLite
$sqlite = Exemplo::factory('SQLite');
In the PHP the Right Way, I saw that, through the Factory
, a class simply creates the object you’d like to use.
I have no example of Abstract Factory
, but I’ve seen it in a course I’ve done. And it’s got me confused.
I would like to know, as simply as possible, what the differences are between these two Patterns (standards).
It would be nice if you took a look here too. The concept of interfaces is new in PHP.
– Leonel Sanches da Silva
In the first sentence of this manual article there is a false statement. Object Interfaces allows code creation that specifies which methods and variables a class must implement ... PHP Does not accept declaring variables in an interface :\
– Wallace Maxters
Are you sure about this? See example 4.
– Leonel Sanches da Silva
@Romaniomorrisonmendez, that is a constant, not a variable. Constants can be put in an interface, variables cannot.
– Wallace Maxters
Maybe if referred to properties. Anyway, it’s an unfortunate statement. Pity I didn’t find it in the documentation.
– Leonel Sanches da Silva
In the English version they do not speak in haha variables, an interface defines which behaviors will be implemented by a class or an interface has no concrete method (it does not implement any behavior) so it makes no sense to have been. If you need to maintain a state of something, an abstract class seems to be more suitable.
– rray
@Wallacemaxters If you’d like to see more details: http://answall.com/q/157466/101
– Maniero