0

I'm running PHP 5.5.9 and I'm getting a parsing error that I haven't a clue how to resolve. Here's an extremely contrieved example of the technique I'm trying to employ:-

<?php

class NumberDisplayer {
    var $numbers = [];

    function __invoke($n) {
        array_push($this->numbers, $n);
        return $this;
    }

    function display() {
        foreach ($this->numbers as $number) {
            echo "$number, ";
        }
    }
}

((new NumberDisplayer())
        (5)
        (10)
        (14)
        (20)
        (11)
        ->display());

?>

That yields:-

Parse error: syntax error, unexpected '(' in <documents>/index.php on line 18

Such a pointless example could obviously be solved via better means, but my question is how to make this technique work in situations where it's a good fit.

My reasoning is as follows: new NumberEchoer() evaluates to an instance of NumberEchoer, which itself is callable due to __invoke. Because of this, I should be able to invoke the expression, which itself returns $this which is also invokable, and so on and so forth. At the end of the expression, I invoke the display method which does the displaying.

I also tried to pack the whole expression onto a single line, but the elimination of the newlines didn't fix anything.

I seem to be able to store NumberDisplayer into a variable and invoke the variable manually on every line, but it makes the code far less readable.

How can this idiom of chaining magic methods be done in a readable fashion?

Louis Jackman
  • 1,001
  • 6
  • 16
  • Why do that? Just curiosity? No, it's not allowed to call functions/methods like this way – Alma Do Mar 27 '14 at 06:11
  • The 'method-chaining' trick can sometimes make APIs flow really nicely, e.g. JavaScript's jQuery. A particular object might have one 'action' that's so common and fundamental that it might as well be directly invoked on the object rather than via a method. That's the theory anyway... – Louis Jackman Mar 27 '14 at 06:17
  • Chaining (fluent interface) is pattern which sometimes is treated as anti-pattern. There are cases when using it may improve architecture & readability, but such call is definitely bad sample – Alma Do Mar 27 '14 at 06:27

1 Answers1

-1

1.. Remove the var because php variables uses $, example $numbers = " ";

2.. Use only one underscore for the invoke name like __invoke.

3.. Edit echo "$number,"; to echo $numbers; as ur empty array in the loop array

SayMaxil
  • 27
  • 8
  • PHP's documentation claims that 'var' is a synonym for 'public' and is no longer deprecated; I get an error without it. The docs also claim magic methods need two underscores. Won't foreach simply not execute if the array is empty? – Louis Jackman Mar 27 '14 at 06:00
  • It will but your declarations are wrong.Public array in PHP needs no var according to the php doc .. I am using my mobile for this. I will post the code when using my PC – SayMaxil Mar 27 '14 at 06:42
  • 1. You need either `var` or `public`, `protected` or `private` there, you cannot just remove that keyword. 2. The magic method name is defined as `__invoke`, you can't just rename it and still get the same effect. – deceze Mar 27 '14 at 09:43