1

I found the entry PHP OOP - toString not working but it does not address my issue as I believe I am calling the magic method properly...

Here is my code:

class Alerts {

    public $message;
    public $type;
    public $output;

    public function __construct($message_id)
    {
    include 'con.php';          
    $stmt = $conn->prepare(
    'SELECT * FROM alerts WHERE id = :message_id');
    $stmt->execute(array(':message_id' => $message_id));

    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $this->type = $row['type'];
        $this->message = $row['message'];  
    }
    }

    public function __toString (){
        $output ='';
        $output .= "<div class='" . $this->type . "'><button class='close' data-dismiss='alert'></button>";
        $output .= "<i class='fa fa-check-circle'></i>&nbsp;<strong>";
        $output .= $this->message;
        $output .= "</strong> </div>";
        return $output;
     } 

}

It works if I call:

$message_id = 6;

$alert = new Alerts($message_id);
$output ='';
$output .= "<div class='" . $alert->type . "'><button class='close' data-dismiss='alert'></button>";
$output .= "<i class='fa fa-check-circle'></i>&nbsp;<strong>";
$output .= $alert->message;


$output .= "</strong> </div>";

on the page but not if I use:

$message_id = 6;

$alert = new Alerts($message_id);
echo $alert->output;

I am a newbie with PHP OOP so you help is greatly appreciated

3
  • 5
    The __toString() method is automatically called when an object is used in the context of a string. Just do echo $alert; Commented Jan 21, 2015 at 16:12
  • 3
    Not sure why you think you need ->output... No language I know uses that to invoke toString. Commented Jan 21, 2015 at 16:12
  • 1
    Also don't forget if you're trying to actually modify the public property $output, you must use $this->output in the context of a method. Commented Jan 21, 2015 at 16:17

1 Answer 1

1

From the PHP docs:

The __toString() method allows a class to decide how it will react when it is treated like a string. For example, what echo $obj; will print. This method must return a string, as otherwise a fatal E_RECOVERABLE_ERROR level error is emitted.

By that logic, doing echo $alert->output; will just output the blank property public $output; that was declared in your class. Two reasons that public $output is blank and doesn't get modified is:

  1. You don't access $this->output in the context of the __toString() method.
  2. __toString() doesn't get called when you access any method or property of the object, only when you treat an object like a string.

What you actually should do if you decide to utilize __toString() is:

echo $alert;
Sign up to request clarification or add additional context in comments.

9 Comments

Hi sjagr, Thanks you for your reply I did as you suggested and used echo $alert and it printed out but the I get the error after: Catchable fatal error: Method Alerts::__toString() must return a string value in /var/www/vhosts/dev-roe-vti.org/httpdocs/mdashboard/login.php on line 130
@janetspagnol You're welcome. If my solution helped you, it is appropriate to mark off the solution as accepted by clicking the grey checkmark below the up/down arrows to the left of my answer so that it turns green. See this meta article for more info
Here is the var_dump if it helps object(Alerts)#3 (2) { ["message"]=> string(78) "We are sorry but we do not have a Senior Mentor that matches these credentials" ["type"]=> string(18) "alert alert-danger" }
@janetspagnol Did you modify your __toString() method? As the error says, you must be returning something other than a string. Either way, this should be addressed in a new question as it is unrelated to this question.
@janetspagnol The var_dump is not helpful at all. What is in your __toString() method?
|

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.