0

I have an object created within a method, and I would like to further process it in another method of the same class.

I tried it like this, but $obj is not passed through to method two():

class SomeClass {
    public static function one($id)
    {
        $obj = new self();
        $obj->id = $id;
        //...
        return $obj;
    }

    public function two()
    {
        $obj->id = 2;
        return $obj;
    }
}

$obj = SomeClass::one($id)->two();

Is there a way to achieve this?

I found this thread How to chain method on a newly created object?, but it's still not quite clear whether this is possible or not.

Community
  • 1
  • 1
orszaczky
  • 13,301
  • 8
  • 47
  • 54

3 Answers3

2

two is a static method. You're trying to invoke it on an instance, and static methods by definition don't work on a specific instance.

Make the method not static, and manipulate $this instead of $obj.

user229044
  • 232,980
  • 40
  • 330
  • 338
1

If I'm not misunderstanding what you're trying to do, if you don't make the second method static, you can return an object and it'll be passed in as $this in the chained call;

class SomeClass {

    public $id = 0;

    public static function one($id)
    {
        $obj = new self();  // Create and return new object
        $obj->id = $id;
        return $obj;
    }

    public function two()
    {
        $this->id = 3;      // The new object is $this here
        return $this;
    }
}

$obj = SomeClass::one(5)->two();
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Excellent, this is exactly what I was looking for! Thank you! The second static function was by mistake, I just copied the first method and accidentally forgot it there (I corrected it). I didn't know I could refer to the $obj as $this in the chained call. – orszaczky Dec 07 '13 at 14:59
0

Method chaining only makes sense when you are using setters. In combination with PHPDoc most IDE's (like NetBeans) will support code completion/autocomplete feature again.

p.s note that method chaining can give your more chance on errors like PHP Fatal error: Call to a member function .... on a non-object in .... when mixing in more classes with returning

class Example {

    protected $var = null;


    public function __construct() {

    }

    public function __destruct() {
        unset($this->var); // explicit memory clearing 
    }

    /**
     * @return \Example
     */
    public static function create() {
        return new self();
    }

    /**
     * @return \Example
     */    
    public static function getInstance() {
        return self::create();
    }

    /**
     * @return \Example
     */    
    public static function newInstance() {
        return self::create();
    }


    /**
     * 
     * @param mixed $var
     * @return \Example
     */
    public function setThing1($var) {
        $this->var = $var;
        return $this;
    }

    /**
     * 
     * @param mixed $var
     * @return \Example
     */
    public function setThing2($var) {
        $this->var = $var;
        return $this;
    }    
}

Example::create()->setThing1(1)->setThing2('2');
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34