0

I've done some research and found this SO-Post and this Php Docs...

Now my problem is, it's not working!

My current php version is 5.4.12 which this feature that I'm after is compatible with 5.4. I'm wondering why it's not working?

This is what I'm after and the stuff I've tried so far...

For example, we have a class of:

class MyClass
{
    private $var = "";

    public function __construct()
    {
        // either with or without
        // return $this;
    }

    public static function newObj()
    {
        return new self;
    }

    public function setVar($var)
    {
        $this->var = $var;
    }
}

And I've tried these:

$obj = (new MyClass)->setVar("foo");

// or

$obj = MyClass::newObj()->setVar("foo");

But none work and when I get the $obj value using var_dump, it's just null.

EDIT

I've also tried and placed an echo inside the __construct to see if the class is being initialize, and it is.

Community
  • 1
  • 1
Craftein
  • 762
  • 5
  • 10
  • 3
    You're not returning anything from `setVar()` - what value do you *expect* `$obj` to have? – DCoder Feb 23 '14 at 14:48
  • ah, so i need to have `return $this;`. it works when i put that there, but why? i expect during the instantiation of the class, the object is created/returned and i just access it with `setName` and store the class with the altered name in my local `$obj` variable. so, this is not what it's really happening. thanks. – Craftein Feb 23 '14 at 14:52
  • During the *instantiation*, the object is returned, but you then take it and immediately use it to call `setVar()`. Assignment to `$obj` happens only after the right side of `=` is completely evaluated, and in this case the result of that evaluation is the result of `setVar()`. – DCoder Feb 23 '14 at 14:56
  • return `$this` in a setter method and assigning this in a concatenated statement to a variable holding the entire instance is obscure code. Very likely to break in the next maintenance release ... – Axel Amthor Feb 23 '14 at 14:58

1 Answers1

2

As setVar is not returning anything, the left hand site must be null.

$obj = new MyClass();
$obj->setVar("foo");

will work.

Other way, but quite unusual since setters normally do not return anything:

...
public function setVar($var)
{
    $this->var = $var;
    return $this;
}
...

// now this will work:
$obj = (new MyClass)->setVar("foo");

but for me this is obscure code.

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
  • guess he wants to have chain functions maybe. – Mohammed Joraid Feb 23 '14 at 14:58
  • chain functions: no speed gain, but very likely to get unreadable code. – Axel Amthor Feb 23 '14 at 15:00
  • Methods returning `$this` allow for method chaining: `$some_rows = (new DatabaseQuery)->setFrom(...)->setWhere(...)->setLimit()->execute()` is readable, especially with newlines. But IMO that only works in cases where you don't need to capture the newly constructed object for future usage. – DCoder Feb 23 '14 at 15:00
  • @DCoder: Agree! but I wouldn't expect a setter method to return the object in order to use this in a chain with the constructor. Your example above makes sense, there you get a result set from the last method. – Axel Amthor Feb 23 '14 at 15:03