A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearGiving PHP object-type arguments a default value - page 1
User InfoPosts
Giving PHP object-type arguments a default value#1
I'm writing a PHP application with testability in mind, so my classes always ask in their constructors for the "collaborator objects" they depend on, in respect to the Dependency Injection pattern.

That way, I'm able to pass in mocks or test implementations in my unit tests.

What I want to achieve though, is to be able to instantiate a class with null values passed to its constructor arguments, in order to trigger a fallback mechanism which instantiates a default implementation class for each collaborator.

Because object-type parameters cannot be given a default value in PHP, I have to do it inside the constructor. The following code is an example of the approach I'm currently using :

class Engine
{
private $loader;
private $logger;

public function __construct(ResourceLoader $loader = null, Logger $logger = null)
{
if ($loader == null) $loader = new DefaultResourceLoader;
if ($logger == null) $logger = new DefaultLogger;

$this->loader = $loader;
$this->logger = $logger;
}
}


What do you think of this ? Should I use an IoC container instead or is there another way of giving object-type arguments a default value ?

posted date: 2008-12-17 18:33:00


Re: Giving PHP object-type arguments a default value#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-17 18:33:01


Re: Giving PHP object-type arguments a default value#3
Of course you'd better use an IoC/DIP container to do the job. Your code above (the if statements) increase the coupling of the classes. The client class (Engine) should not care what’s implementing the loader and logger roles. That’s the factory’s or DIP container work to know this. By putting a concrete class name into your code and bind the classes together making your code less flexible (agile), less reusable and thus less maintainable.

posted date: 2008-12-17 23:07:00


Re: Giving PHP object-type arguments a default value#4
Thanks for your answer. Have you got any recommandation for a good PHP container ?

posted date: 2008-12-18 08:06:00


Re: Giving PHP object-type arguments a default value#5
You could use my tiny library ValueResolver in this case to do it more compact

posted date: 2015-07-09 02:48:00


Re: Giving PHP object-type arguments a default value#6
You could use my tiny library ValueResolver in this case, for example:$logger = ValueResolver::resolve($logger, new DefaultLogger);and don't forget to use namespace use LapaLabs\ValueResolver\Resolver\ValueResolver;There are also ability to typecasting, for example if your variable's value should be integer, so use this:$id = ValueResolver::toInteger('6 apples', 1); // returns 6$id = ValueResolver::toInteger('There are no apples', 1); // returns 1 (used default value)Check the docs for more examples

posted date: 2015-07-09 02:49:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.