kocoten1992's avatar

__construct to default value argument?

class A
{
    public function __construct()
    {
            $this->default = 'the default';
    }

    public function one($args = $this->default) //here
    {
        ...
    }

    public function two($args = $this->default) //here
    {
        ...
    }
}

I want to change the default value only one place, and it update in every place, how can I implement that?

0 likes
8 replies
tomopongrac's avatar

I dont understand question but you probably wont something like this ...

class A
{

    private $default;

    public function __construct($default = 'the default')
    {
            $this->default = $default;
    }

    public function one() //here
    {
        $this->default = "some other value"; // if you wont change value
    }

    public function two() //here
    {
        $this->default = "some other value"; // if you wont change value
    }
}
1 like
SaeedPrez's avatar
Level 50

You don't really need to __construct() if you're not sending a value when instancing the class.

class A
{
    protected $default = 'Hello World';

    public function one($default = null)
    {
        $default = ! is_null($default) ? $default : $this->default;
    }

    public function two($default = null)
    {
        $default = ! is_null($default) ? $default : $this->default;
    }

}
1 like
bobbybouwmann's avatar

You can do it like that, something like this is possible though

public function __construct()
{
    $this->default = 'something';
}

public function one($param = null)
{
    $this->setDefaultParam($param);

    // Your code here
}

public function two($param = null)
{
    $this->setDefaultParam($param);

    // Your code here
}

protected function setDefaultParam($param = null)
{
    if ($param) {
        $this->default = $param;
    }
}

You can also always give the variable to the constructor instead

public function __construct($param = null)
{
    $this->default  = $param ?? 'default'; // Note that this only works in PHP 7 or higher
}
1 like
kocoten1992's avatar

Thanks everyone, I like your method, @SaeedPrez, but still wish for something like previous, it short 1 line on every methods though.

SaeedPrez's avatar

@kocoten1992 you could do as @bobbybouwmann suggested and move the logic to getter/setter methods..

class A
{
    protected $default = 'Hello World';

    public function one($default = null)
    {
        $default = $this->getDefault($default);
    }

    public function two($default = null)
    {
        $default = $this->getDefault($default);
    }

    protected function getDefault($default = null)
    {
        // We put the logic here instead
        return ! is_null($default) ? $default : $this->default;

        // If you're running PHP 7+, you could use this instead
        return $default ?? $this->default;
    }
}
kocoten1992's avatar

That is just introduce a new function AND two extra lines, haha :P

SaeedPrez's avatar

@kocoten1992 Yes, but fewer lines does not automatically mean better code or better practice. These few extra lines actually apply the single responsibility principle to your code.

1 like
tomopongrac's avatar

the most important is that the code clear and obvious ...

... and when and when you watch again that code you know what exactly code is doing

Please or to participate in this conversation.