1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Transitioning to print_r

Use print_r to get past the var_dump depth level issues.
  1. school.jpg
    While tinkering with new code on a test project, I asked Bob from Xen Addons for some advice and help. His answers led to more questions, however, there were several gold nuggets in the answers. Due to my reliance on var_dump, the treasure was missed until this morning when I used print_r instead of var_dump.
    1. There is a need to use join and a few other items in the option arrays.
    2. Prepare methods in Models have important value.
    $fetchOptions and $conditions
    The original code included little use of the $fetchOptions and $conditions.

    1. $conditions = array();
    2. $fetchOptions = array( 'limit' => 1);
    The advice was to use join. This took me some time to look at the constants located at the top of Thread.php model in XenForo as well as some other addons but I slowly got the hang of it.

    As a side note, after interpreting the advice and doing some massive tinkering on my test project, it was clear the widget code in XenWord needed updating to include the use of join. I'll write about that in the XenWord DevLog. But for now, you can see the differences in the original lines and the second.

    1. $conditions = array(
    2.    'discussion_state'        => 'visible',
    3.    'permissionCombinationId' => $permissionCombinationId
    4. );
    6. $fetchOptions = array(
    7.    'join'           => XenForo_Model_Thread::FETCH_FORUM
    8.                        | XenForo_Model_Thread::FETCH_USER,
    9.    'limit'          => $instance['numberposts'] * 3,
    10.    'order'          => 'thread_id',
    11.    'orderDirection' => 'desc'
    12. );
    More important, adding the join helped eliminate having to instantiate a new class for the node_title because it was included in the returned array.


    This morning's tinkering led to something even more interesting than using join. I'm a var_dump junky. It's how my code moved from Cowboy to "OK. Try this idiot." In my view, the best part of building something is figuring out how to unravel an array, pulling from Models, and not relying on Google search for answers to copy. It's all about thinking for myself.

    Part of the second advice, though, was to use prepare methods found in the Model. Bluntly, I hadn't even considered it. I didn't even understand it. I tried a few things yesterday, got annoyed, and tinkered with a few other items. This morning was totally different.

    In looking at the var_dump before the prepare and after showed a size difference in the array. The depth of var_dump wasn't great enough to see all the elements. I only noticed it when looking at the size difference of the arrays. Afterwards I noticed the message stating there were more elements...

    To overcome the depth issue, I inserted in a print_r and immediately saw elements that were not being shown with the var_dump. Sure enough, many of the elements I needed for my little test project were in the array after using the prepare.

    After more tinkering, I decided to make a dump.php file and require the file in my phptest files. The file contains two functions one function with a conditional to check if the variable is an array.

    1. <?php
    3. /**
    4.  * Filename: dump.php
    5.  *
    6.  * Return information about a variable.
    7.  * Include this file to the top of phptest files.
    8.  *
    9.  * Usage: dd($nameOfVariable);
    10.  */
    12. function dd($variable) {
    14.   if ( is_array( $variable ) ) {
    16.     echo '<pre>';
    17.     print_r($variable);
    18.     echo '</pre>';
    20.     return true;
    21.   }
    23.   return var_dump($variable);
    24. }
    26. // End dump.php
    Now I can simply put dd($nameOfVariable); and out will spit a pretty array or a var_dump. Sweet.


To make a comment simply sign up and become a member!