rip747
rip747

Reputation: 9443

translate referenced arrays in php to coldfusion

I'm in the process porting over Liquid from PHP to Coldfusion. I'm having some problems as in PHP you can pass around arrays by reference where in ColdFusion you can not do this. What I'm wondering if anyone has any experience translating PHP to ColdFusion and has ran into this problem and what solution they used to work around it.

As an example of the problem I'm having, consider this:

class LiquidTagIf extends LiquidDecisionBlock
{

    private $_nodelistHolders = array();

    private $_blocks = array();

    public function __construct($markup, &$tokens, &$file_system)
    {
        $this->_nodelist = &$this->_nodelistHolders[count($this->_blocks)];
        array_push($this->_blocks, array(
            'if', $markup, &$this->_nodelist
        ));
        parent::__construct($markup, $tokens, $file_system);
    }

the lines I'm having problem translating is this one:

$this->_nodelist = &$this->_nodelistHolders[count($this->_blocks)];

and this one:

'if', $markup, &$this->_nodelist

All three of these variables(_nodelist, _nodelistHolders, _blocks) are arrays. While _nodelistHolders and _blocks are declared in the LiquidTagIf class, _nodelist is declared in a parent class called LiquidTag (the inheritance chain is LiquidTagIf -> LiquidDecisionBlock -> LiquidBlock -> LiquidTag)

Upvotes: 0

Views: 166

Answers (1)

Mister Dai
Mister Dai

Reputation: 816

As usual, for most problems, Ben Nadel has already blogged the answer :/

http://www.bennadel.com/blog/275-Passing-Arrays-By-Reference-In-ColdFusion-SWEEET-.htm

Basically, a ColdFusion array is actually based upon he Java object "java.util.List". To pass an array by reference, you'll need to create them as "java.util.ArrayList" instead. They'll still work just as you'd expect with all the array functions (like ArrayAppend), but when you pass them to a function, it'll be by reference.

<cfset arr = CreateObject(
"java",
"java.util.ArrayList"
).Init() />

Upvotes: 3

Related Questions