2

So i was wondering if there is a way to method chain, when the initial method is a static function. Here is what I mean:

    class foo
    {
        public static function a()
        {
            $foo = new foo;
            return $foo->bar(); 
        }

        public function bar()
        {
            return $this;
        }

        public function b()
        {
            return 1;
        }
    }

    print foo::a()->b();

EDIT print foo::a()->b(); not print foo:a()->b();

7
  • 2
    You should not be getting an array anywhere with this code, I don't think this the actual code you're using. Besides, you are not calling function bar(), you are calling property bar. Demo of this working: codepad.org/5kPLusX8 Commented Jun 22, 2011 at 5:34
  • That was just a typo on my end. And no, this is not the actual code. Just a conceptual question Commented Jun 22, 2011 at 5:37
  • There were a few typos, but that's not what I was referring to. In function a(), you aren't returning bar(), you are returning bar. Something in your testing is off, you should have taken the time to at least share the code you meant, or something that actually works to demonstrate the issue. Commented Jun 22, 2011 at 5:38
  • 1
    After adding the missing method call and semicolon ($foo->bar();) this code works for me and gives 1. What do you get? Commented Jun 22, 2011 at 5:41
  • 1
    Now it looks like if it would work. foo::a()->bar()->bar()->b(); even if pointless. Commented Jun 22, 2011 at 5:47

3 Answers 3

1

Static Methods or Other Methods, as long as the method is returning an object either self or some other, the methods can be chained, with the same method you are attempting.

class foo {
   public function __construct() {
   }
   public function create() {
       // create something;
       return $this;
   }
   public function performSomethingElse() {
      // perform something
      return $this;
   }
}
$object = new foo;

$object -> create() -> performSomethingElse();
Sign up to request clarification or add additional context in comments.

Comments

0

this line

print foo:a();

should be

print foo::a();

and you will not be able to return $this in a static method it needs to be instantiated first:

$foo = new Foo();
print $foo->a()->b();

1 Comment

yes i see your update, there is a difference between $foo->bar(); and $foo::bar(); in the second one you can't return $this. thats why i used new Foo();
0

Only sort of an answer, and somewhat idiosyncratic:
But I would advise that you have your object accompanied by a factory procedure instead:

 class foo { .... }

 function foo() { return new foo; }

This might remove some of the confusion for you. And it even looks a bit nicer by avoiding the mix of static and object method calls:

 foo()->bar()->b();

It basically externalizes the static function. And your object only implements the chainable methods which return $this, or actual results.

1 Comment

That's the purpose of the separate function. It does instantiate a new foo;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.