• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print_r

#1
[eluser]zilverdistel[/eluser]
hi, i'm using CI a couple of months now. I'm trying to use print_r to display an object (instantiated from a model). As a result, the whole CI-object is displayed. Obviously, I don't want this, so any ideas on this would be great. On the other hand, I'd also like to know what's happening. I'm relatively new to OOP (1 year).

Code:
print_r($this->modelname);

#2
[eluser]xwero[/eluser]
try using var_dump it displays more information

#3
[eluser]zilverdistel[/eluser]
i did var_dump, and indeed, I get more.

Code:
object(Applicatie)#15 (17) {
  ["gebruiker"]=>
  bool(false)
  ["referrer:private"]=>
  string(0) ""
  ["taal:private"]=>
  string(2) "nl"
  ["taal_voluit:private"]=>
  array(2) {
    ["nl"]=>
    string(10) "nederlands"
    ["fr"]=>
    string(8) "francais"
  }
  ["_parent_name"]=>
  string(10) "Applicatie"
  ["_ci_scaffolding"]=>
  &bool;(false)
  ["_ci_scaff_table"]=>
  &bool;(false)
  ["config"]=>
  &object;(CI_Config)#3 (2) {
    ["config"]=>
    &array;(33) {
      ["base_url"]=>
      string(25) "http://localhost/rapport/"
      ["index_page"]=>
...

The problem is that I only want to display the properties and methods that I defined in my model, without these references to CI_Objects. Is there an easy way to do this, without having to know what properties and methods are defined in the model?

#4
[eluser]Seppo[/eluser]
Will something like this do it?
Code:
function debug()
    {
        print_r(
            array('properties' =>
                array_diff(
                    array_diff(
                        array_diff(
                            get_object_vars($this),
                            get_class_vars('Model')
        
                        ),
                        get_object_vars(get_instance())
                    ),
                    array('_parent_name' => ucfirst(__CLASS__))
                ),
                'methods' =>
                array_diff(get_class_methods(__CLASS__), get_class_methods('Model'))
            )
        );
    }

#5
[eluser]zilverdistel[/eluser]
Tnx, I tried it by putting it in my model as a method, but when i call it from my controller (like [/code]$this->modelname->debug()[/code]) I got errors like this:

Code:
A PHP Error was encountered

Severity: 4096

Message: Object of class CI_Benchmark could not be converted to string

Filename: models/gebruiker.php

Line Number: 51


A PHP Error was encountered

Severity: 4096

Message: Object of class Gebruiker could not be converted to string

Filename: models/gebruiker.php

Line Number: 51
...

do you have any suggestions on how to use it?

#6
[eluser]Seppo[/eluser]
You can place an @ before print_r to avoid those errors (I think it's only on php 5) and it should be working...

#7
[eluser]zilverdistel[/eluser]
[quote author="Seppo" date="1205355709"]You can place an @ before print_r to avoid those errors (I think it's only on php 5) and it should be working...[/quote]

excellent, this works in my browser, but now when i tried to write this to my log files, I still get the errors (only in the logs, not in the browser).

I changed
Code:
print_r(....)
to
Code:
return print_r(... , true)
in the debug() function and used
Code:
log_message('debug',$this->modelname->debug());
.

#8
[eluser]Seppo[/eluser]
You can use this longer - most reliable code, but it will only work on PHP 5.
Code:
function debug()
    {
        print_r(
            array('properties' =>
                $this->array_obj_diff(
                    $this->array_obj_diff(
                        $this->array_obj_diff(
                            get_object_vars($this),
                            get_class_vars('Model')
        
                        ),
                        get_object_vars(get_instance())
                    ),
                    array('_parent_name' => ucfirst(__CLASS__))
                ),
                'methods' =>
                $this->array_obj_diff(get_class_methods(__CLASS__), get_class_methods('Model'))
            )
        );
    }

    function array_obj_diff($array1, $array2)
    {
        $args = func_get_args();
        $args[] = array(&$this, 'str_cmp');
      
        return call_user_func_array('array_udiff',$args);
    }

    function str_cmp($a, $b)
    {
        return strcmp(serialize($a), serialize($b));
    }

#9
[eluser]zilverdistel[/eluser]
thanx thanx thanx (x1000)!!!

this really makes my day! i've been banging my head against the wall for this ;-)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.