0

I saw a class in Laravel where "new static()" is being called to create object chaining as opposed to "return $this" which is what i'm familiar with seeing.

Does anyone know why this approach is taken and is there a term for it? file:

vendor\laravel\framework\src\Illuminate\Support\Stringable.php
class Stringable implements JsonSerializable
{
    protected $value;

    public function __construct($value = '')
    {
        $this->value = (string) $value;
    }

    public function camel()
    {
        return new static(Str::camel($this->value)); // <-- why not "return $this;" instead?
    }
}

Trying to understand the approach.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • 1
    Please take a look at this https://stackoverflow.com/questions/5197300/new-self-vs-new-static – xenooooo Dec 28 '22 at 02:50
  • @xenooooo The link you shared seems to be about something else. I'm wondering why he's using this approach when usually you can return $this pointer to the already created object. – user2123073 Dec 28 '22 at 03:06
  • 2
    they don't want to return the current object they want to return a new instance containing the transformed string – lagbox Dec 28 '22 at 03:08
  • @lagbox Why though? Both approaches achieve the same thing but creating a new object will cause more load on the server. – user2123073 Dec 28 '22 at 03:13
  • 2
    Laravel takes this approach with other things too. Collections methods rarely mutate the original collection. – IGP Dec 28 '22 at 04:26
  • @IGP I've heard something similar as well. I wonder what the benefit is. – user2123073 Dec 28 '22 at 04:28
  • Does this answer your question? [Why return new static? (PHP)](https://stackoverflow.com/questions/37460592/why-return-new-static-php) – steven7mwesigwa Dec 28 '22 at 04:34
  • Does this answer your question? [New self vs. new static](https://stackoverflow.com/questions/5197300/new-self-vs-new-static) – RG Servers Dec 28 '22 at 04:36
  • @steven7mwesigwa No it doesn't. It's on a different topic. – user2123073 Dec 28 '22 at 04:40
  • @KGG No it doesn't. – user2123073 Dec 28 '22 at 04:41
  • @IGP your answer seems close to what this could be. Am looking into it. https://blog.sapegin.me/all/avoid-mutation/ – user2123073 Dec 28 '22 at 04:41
  • @IGP Your answer was the closest to being correct. If you create a reply I'll accept the answer but link an article in your answer. This could help other people. – user2123073 Dec 28 '22 at 05:33

1 Answers1

1

This is an alternative way of return $this; but is used not just to return the object. It modifies the object before its returns.


In detail, new static() is used to create an object of the same class. In your Stringable class. So when you use it in your code __construct method will call automatically.

Example

class MyClass
{
    protected $value;

    public function __construct($value)
    {
        $this->value = $value;
    }

    public function add($value)
    {
        $this->value += $value;

        return new static($this->value);
    }
}

$obj = new MyClass(5); # retuns 5 -> Via __construct init
$obj2 = $obj->add(10); # retuns 15 -> Via __construct initiated value + New value 
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85