phparrayscakephpcakeemail

Trying to dump Object or Array in CakePHP


I am trying to dump an object or an array into the debug while coding on CakePHP. I have been trying a variety of commands, and I've honestly lost track of the various outputs. I've definitely seen an output indicating the object type (CakeEmail) and I've seen an array. Unfortunately I haven't been able to recreate either one of those.

Here's the code I'm using, with some comments to indicate commands that have definitely failed.

public function _sendEmail($template, $replace_content, $to, $from = null)
{
    App::uses('CakeEmail', 'Network/Email');
    $this->Email = new CakeEmail();

    //Preparation of variables to insert into Email
    $this->Email->to($to);
    $this->Email->subject($subject);
    $this->Email->emailFormat('both');

    //Debugger::log($this->Email); //Worked - Shows certain output in file
    //Debugger::dump($this->Email); //Partial - Did not show any output in file, but did not fail to run.
    //Debugger::trace($this->Email); //Partial - Did not show any output in file, but did not fail to run.
    //Debugger::debug($this->Email); //Failed - function did NOT continue

    //dump($this->Email); //FAILED
    //trace($this->Email); //FAILED
    //debug($this->Email); //Partial - Did not show any output in file, but did not fail to run.

    //Debugger::log(">>log: ".$this->Email); //unable to show variable. Only heading string was output to file
    //Debugger::dump(">>dump: ".$this->Email); //Partial - Did not show any output in file, but did not fail to run.
    //Debugger::trace(">>trace: ".$this->Email); //NOT TESTED

    //Debugger::log(Debugger::dump($this->Email)); //Worked - Shows certain output in file - dump returns NULL
    //Debugger::log(">>log: headers: ".Debugger::dump($this->Email->getHeaders())); //Worked - Shows certain output in file - dump returns NULL
    //Debugger::log(">>log: headers: ".Debugger::dump($Email->getHeaders())); //FAILED - Undefined variable

    include 'dkim.php';
    $newHeader = AddDKIM($from_email, $to, $subject, $content);

    if (!empty($newHeader)) {
        Debugger::log ("newHeader is: $newHeader\n;Adding it to email.");
        $emailHeaders = $this->Email->getHeaders();
        Debugger::log ($emailHeaders);
        debug($emailHeaders);
        //$emailHeaders = $newHeader.emailHeaders;
        //Debugger::log($this->Email->getHeaders());
        debug ($emailHeaders);
    }
    else
        Debugger::log ("No new Header to Add.");

    $this->Email->send($content);
}

Solution

  • Im not entirely familiar with CakePHP but you can use print_r($variable, true) which returns a dump of the variable in a string. You can then use that in you log function.

    Debugger::log(">>log: ".print_r($this->Email, true));