Dynamic factory pattern
Posted: Wed Apr 01, 2009 6:35 pm
Hi
I'm a newbie in PHP language, although i'm very familiar with java and OO languages. I already check that php don't support var typing and class cast, that is very strange for me, for exempl i'm used to have interfaces and cast it implementation to that interface, e.g.
JAVA:
public interface animal { ... }
public dog implements animal { ... }
Animal a = new Dog();
However i didn't find a way to do this in PHP, in my case i want to do a dynamic factory class:
So i've defined the following user class:
And after I’ve defined the UserImplementation in other folder (business) that extends User and implement all abstract methods.
But I want to cast the new class to User in order to getInstance method give me User classes. I want to do something like the highlighted code:
So when I do User::getInstance() I’m really get User object and not UserImplementation object.
Do you have any idea that could help me on this? How can i do dynamic factoring in php separating the specification layer from business layer, getting this layers completely independent between them?
Thanks in advance.
Chris Corbyn | Please wrap your PHP code inside
I'm a newbie in PHP language, although i'm very familiar with java and OO languages. I already check that php don't support var typing and class cast, that is very strange for me, for exempl i'm used to have interfaces and cast it implementation to that interface, e.g.
JAVA:
public interface animal { ... }
public dog implements animal { ... }
Animal a = new Dog();
However i didn't find a way to do this in PHP, in my case i want to do a dynamic factory class:
So i've defined the following user class:
Code: Select all
abstract class User{
// The parameterized factory method
public static function getInstance()
{
if (include_once $implementation_class.'.php') {
//get the classname from configuration constant
$implementation_class = “business/UserImplementation”;
$classname = substr($implementation_class,strrpos($implementation_class, "/")+1, strlen($implementation_class));
return new $classname;
} else {
throw new Exception ('The class ' .$implementation_class . ' was not found in configuration file.');
}
}
abstract getUserName();
abstract getEmail();
...
}
And after I’ve defined the UserImplementation in other folder (business) that extends User and implement all abstract methods.
But I want to cast the new class to User in order to getInstance method give me User classes. I want to do something like the highlighted code:
Code: Select all
abstract class User{
// The parameterized factory method
public static function getInstance()
{
if (include_once $implementation_class.'.php') {
//get the classname from configuration constant
$implementation_class = “business/UserImplementation”;
$classname = substr($implementation_class,strrpos($implementation_class, "/")+1, strlen($implementation_class));
return new (User) $classname;
} else {
throw new Exception ('The class ' .$implementation_class . ' was not found in configuration file.');
}
}
abstract getUserName();
abstract getEmail();
...
}
Do you have any idea that could help me on this? How can i do dynamic factoring in php separating the specification layer from business layer, getting this layers completely independent between them?
Thanks in advance.
Chris Corbyn | Please wrap your PHP code inside
Code: Select all
and [/ php][/b] tags when posting on the forum[/color]