Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "variables"

Retrieving action variables in a component

Used in a component action, the following code can be used to get the variables set in a module action that has used the component in its view:

$actionStack = $this->context->getActionStack();
$variables = $actionStack->getFirstEntry()->getActionInstance()->getVarHolder();
 

then you can use

$foo = $variables->get("foo");
 

to retrieve a variable. Note that the above is for the most recent action only.

by Rich Sage on 2009-10-01, tagged action  component  variables 

debug objects or variables...

my approach to the previous snippet is to create a class called debugTools.class.php and throw it in the \lib directory. It should contain the following code:

<? 
 
Class debugTools {
 
/**
* renders to the page a proper object map for looking over contents of an object as it occurs.
*
* @param    object       $object        (required) the object for inpection
*/
Public Static function inspect($object)
    {
    echo "<PRE>";
    print_r($object);
    echo "</PRE>";
    }
 
 
}// ends class  
?>

you can then call it easily (with only two or three keystrokes if your IDE does autocompletion) by calling:

echo debugTools::inspect($object);

the resulting formating looks like so (i used it to analyze a usp shipping object):

ups Object
(
    [_errors] => Array
        (
        )
 
    [_action] => 3
    [_delivery_code] => GND
    [_src_country] => US
    [_dst_country] => US
    [_rate_chart] => 5
    [_container] => 0
    [_rescom] => 1
    [_rate_charts] => Array
        (
            [0] => Regular+Daily+Pickup
            [1] => On+Call+Air
            [2] => One+Time+Pickup
            [3] => Letter+Center
            [4] => Customer+Counter
            [5] => Drop+Off
        )
 
    [_containers] => Array
        (
            [0] => 00
            [1] => 01
            [2] => 21
            [3] => 22
            [4] => 23
            [5] => 1
            [6] => 2
        )
 
    [_rescoms] => Array
        (
            [0] => 1
            [1] => 2
        )
 
    [_src_zip] => 97499
    [_dst_zip] => 90120
    [_weight] => 2
)

a handy way of viewing objects that can be accessed from the layout or pretty much anywhere, if you're new to symfony use it to look over $sf_user in the template, you will have a much better idea what's going on.

by Russ Cann on 2006-06-02, tagged debug  object  variables 
(1 comment)

Dump variables/objects to screen

Sometimes a set of circumstances will arise when the programmer is not certain what objects are available to a particular point of a symfony app (the manual is, at the time of writing, not entirely exhaustive).

Simply pop this code in your code temporarily and the output should give you some clues.

$vars = get_defined_vars();
foreach($vars as $key => $val) {
  $t = gettype($$key);
  echo "${key} (${t})<br />";
}
by halfer on 2006-06-02, tagged debug  objects  variables 
(1 comment)