Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9294355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:21:10+00:00 2026-06-18T21:21:10+00:00

Please, consider the following example: template.php: <?php echo $vars[‘arr’]; echo ——– ; echo $vars[‘obj’];

  • 0

Please, consider the following example:

template.php:

<?php

echo $vars['arr'];

echo " -------- ";

echo $vars['obj'];

?>

test.php:

<?php
$file = "template.php";
$vars = array( 'arr' => array(), 'obj' => new StdClass() );  

var_dump( json_encode($vars)  );  

function loadFile($file, $vars)
{
    try 
    {
        if (is_array($vars) && !empty($vars)) {
            extract($vars);
        }

        ob_start();
        include $file;
        return ob_get_clean();
    } 
    catch (Exception $e) 
    {
        return false;
    }
}

loadFile($file, $vars);

?>

This code will output:

string(19) "{"arr":[],"obj":{}}"
PHP Catchable fatal error:  Object of class stdClass could not be converted to string in template.php

The problem here is, in the template.php I am considering $vars to be an array() however 1 of the elements is an Object as you can see from the json output.

Adding a simple checking in the template to verify if the ekement is an array or not would solve the problem, however I would need to this to multiple elements, elements, so, not very good =) so, I trying to find a way to prevent the error in the moment of binding the template and $vars.

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:21:11+00:00Added an answer on June 18, 2026 at 9:21 pm

    simply turn error_reporting off while parsing:

    $old_level = error_reporting(0); // no errors, preserve old error reporting level
    ob_start();
    include $file;
    $content = ob_get_clean();
    error_reporting($old_level); // reset error reporting level
    return $content;
    

    Note: This will only hide errors that aren’t very critical.

    In order to catch a Catchable Fatal Error, see this question: How can I catch a "catchable fatal error" on PHP type hinting?

    You need to register an error handler using set_error_handler:

    function handleError($errno, $errstr, $errfile, $errline) {
      // handle error
    
      // return true, so the normal php handler doesn't get called
      return true;
    }
    set_error_handler('handleError');
    

    If you want to integrate you handler cleanly into other code which also sets error_handlers, you might consider restoring the error handler afterwards using restore_error_handler

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following example: #include <string> #include <vector> using std::string; using std::vector; template
Please consider the following example code (from the lm doc): ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt
please consider the following code: template <typename T> struct foo { template <typename S>
Please consider the following structure of my website: www.mydomain.com/ www.mydomain.com/index.php www.mydomain.com/404.php www.mydomain.com/css www.mydomain.com/blog/ How
consider the following example: public IEnumerable<String> Test () { IEnumerable<String> lexicalStrings = new List<String>
Please consider the following example struct Foo { int bar; Foo(int i):bar(i){cout << real
Please consider following example: The source image consists of 6 areas that need to
Please consider the following example. Let's say we are building a DB for an
Please consider the following view: <p>Count <span data-bind=text: unreadCount()>&nbsp;</span></p> <div data-bind='template: { name: conversationTemplate,
Please consider the following example def foo(a: Int, b: Int = 100) = a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.