1

I'm trying to print data from an array. The array is from a class. I'm getting

array(0) { }

instead of:

Array ( [0] => header_index.php [1] => footer.php )

The code is:

<?php
class TemplateModel {
    public function getTemplate($template = "index"){
        switch($template){
            case "index":
                $templateconfig = array("header_index.php","footer.php");
                break;
        }
        return $templateconfig;
    }
}
$temodel = new TemplateModel(); 
var_dump(get_object_vars($temodel));
$temodel -> getTemplate();
?>

What i'm doing wrong? Thanks in Advance

1
  • get_object_vars() returns the public properties of an object; not local vars used in its methods Commented Jul 29, 2013 at 16:23

4 Answers 4

1
var_dump(get_object_vars($temodel)); 

will output class member $temodel. There are no class member variables, so output is empty. If you want to output your array, you have to for example do this:

print_r($temodel -> getTemplate());
Sign up to request clarification or add additional context in comments.

Comments

0

My immediate thoughts are it looks like you are setting the variables in the function 'getTemplate' and that is not being called until after the var_dump.

ADD: And I just noticed you are not capturing the return of the function. You are var_dumping the object created from the class.

FIX:

<?php
class TemplateModel {
    public function getTemplate($template = "index"){
        switch($template){
            case "index":
                $templateconfig = array("header_index.php","footer.php");
                break;
        }
        return $templateconfig;
    }
}
$temodel = new TemplateModel(); 
$returned_var = $temodel -> getTemplate();
var_dump($returned_var);
?>

If you want to set the array as a variable of the object, that is a different problem.

Comments

0

It looks like you're not initializing the $templateconfig variable until getTemplate() is called. And you don't call it until after var_dump().

So basically, you're dumping an object that has no initalized member properties which is why you see an empty array.

Comments

0

Your object itself has no variables (properties) to be returned with a call to get_object_vars(). The $templateconfig variable only exists within the scope of the getTemplate() function and is not a property of the object.

If your intent is to make it a property of the object, you should do something like this:

class TemplateModel {
    private $template_config = array(
        'index' => array("header_index.php","footer.php"),
        // add other configs here
    );

    public function getTemplate($template = "index"){
        if(empty($template)) {
            throw new Exception('No value specified for $template');
        } else if (!isset($this->template_config[$template])) {
            throw new Exception('Invalid value specified for $template');
        }

        return $this->template_config[$template];
    }
}

$temodel = new TemplateModel();
var_dump($temodel->getTemplate());

Note here if you call get_object_vars() you still would get an empty array as I have made the $template_config variable private, forcing the caller to use the getTemplate() method to access the template data.

Comments

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.