0

I'm building a class that needs two arguments, and they can be passed through __constructor, or they can be set using setter methods.

What is the best way to check if arguments are passed through constructor?

I did it like this:

class Services {

    public function __construct(array $configs, array $services)
    {
        if(isset($configs) AND isset($services)) {
            $this->configs = $configs;
            $this->services = $services;
        }
    }

    public function setConfigs(array $configs)
    {
        $this->config = $configs;
    }

    public function setServices(array $services)
    {
        $this->services = $services;
    }
}   

Now this works fine, but I'm not 100% if this is the right way. The thing thats bothering me is that if arguments are passed through constructor, I want both of them there, not only one.

How would I prevent user to put only one argument in constructor?

5 Answers 5

5

Currently you have to pass two arguments. To make them optional, you need to assign default values. You can then enforce both with a simple check:

public function __construct(array $configs = null, array $services = null) {
    if ($configs === null xor $services === null) {
        throw new InvalidArgumentException('Supply both or none!');
    }
    if ($configs && $services) {
        $this->setConfigs($configs);
        $this->setServices($services);
    }
}

You shouldn't use isset, since the variable always exists, as it's part of the function signature. You only need to check the values.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you tested it? When you have defined the types in the function header __construct(array $configs, array $services), you ensure that a user MUST pass two arrays!

$test = new Services(array());

fails - Catchable fatal error: Argument 2 passed to Services::__construct() must be an array

$test = new Services('','');

Fails - Catchable fatal error: Argument 1 passed to Services::__construct() must be an array,

only

$test = new Services(array(), array());

is allowed!

Comments

0

Seperately handle the arguments you passed.

public function __construct(array $configs, array $services)
{
    if(isset($configs)) {
        $this->configs = $configs;
    }
    if(isset($services)) {
        $this->services = $services;
    }
}

Comments

0

you can add the php is_null() function, so if !is_null(arg1) && !is_null(arg2), then you're fine.

2 Comments

How about !(is_null(arg1) || is_null(arg2))?
It gets the same result, and the code is equally long. So both ways are correct :)
0

I would do it like this:

public function __construct(array $configs = false, array $services = false)
    {
        if($configs && $services) {
            $this->configs = $configs;
            $this->services = $services;
        }
    }

1 Comment

I would rather not doing this, it is better to don't mix type, you should use array $configs = array() to allow no value to be set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.