0

I am curious in writing a chaining interface in PHP OOP. I modified this sample code from the php.net website, and I want to take it further - how can I return objects or arrays from this kind of interface?

// Declare a simple class
class TestClass
{
    public $foo;

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

    public function __toString()
    {
        return $this->foo;
    }
}

$input = (object)array("title" => "page 1");
$class = new TestClass($input);
echo $class;

error,

Catchable fatal error: Method TestClass::__toString() must return a string value in C:\wamp\www\test\2013\php\fluent_interface.php on line 2

Should I use different magic method instead of __toString then?

EDIT: Can I return this as my result,

stdClass Object ( [title] => page 1 )
1
  • What exactly do you want it to do? Commented Aug 22, 2013 at 13:04

2 Answers 2

1

To get what you want you need to use following syntax:

print_r($class->foo);

The __toString() magic method tries to convert your whole class 'TestClass' to a string, but since the magic method is not returning a string, it is showing you that error. Of course you could also rewrite your __toString() method to do the following:

public function __toString()
{
    return print_r($this->foo, true);
}

http://php.net/manual/en/function.print-r.php

http://www.php.net/manual/en/language.oop5.magic.php#object.tostring

Sign up to request clarification or add additional context in comments.

3 Comments

but it seems I can't access the properties inside the object, for instance, $class = new TestClass($input); echo $class->title; I get this error Notice: Undefined property: TestClass::$title in C:...fluent_interface.php on line 23 instead of page 1... how can I access the data inside the object then?
The foo property in your class contains another object that is your data. In your example you should use this syntax: $class->foo->title
got it by doing that myself before reading your answer. thank you! :D
1

I think you are looking for either print_r or var_export functions:

public function __toString()
{
    return var_export($this->foo, true);
}

and var_export is better since it also return type of value (and, besides, in valid PHP-code format). Note, that __toString() method have nothing common with fluent interface. It's just different things.

3 Comments

thanks but how can I access the data inside the return object then? for instance, echo $class->title; I want to get page 1 as the result. Is it possible?
I'm not sure what are you trying to achieve. To replace your class instance by those, that was passed to constructor? If yes, why?
sorry, I got the answer by doing this echo $class->foo->title; thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.