0

我有一个静态函数,它在进入foreach()循环之前定义了一些变量。
从内部foreach(),我尝试调用我的$pnUuidvar,然后得到

未处理的异常 未定义的变量:pnUuid

如果我立即使用or跟随该打印,我可以在循环之前成功地回显 var ,也可以在循环成功回显。 调用也规避了异常,但是 var 为NULL :(die()continue
global $pnUuid

public static function find( $input ) 
{
    // instantiate
    $resultsByBrand = array();
    $brandDictionary = new BrandHashTable();

    // attempt to get a PN UUID
    $partnumber = PartNumberEntity::where( 'number', '=', $input['partnumber'] )->first();
    $pnUuid = empty($partnumber) ?  false : $partnumber->uuid;

    // get current inventory for the PN
    $rzStocklines = RzStockEntity::where('fullpartnumber', '=', $input['partnumber'])
            ->get( array('fullpartnumber', 'manufacturer', 'quantity'));

    // process the stocklines
    foreach( $rzStocklines AS $row )
    {
        // do our darndest to get a UUID for the Brand
        $brandDictionary->registerByName($row->manufacturer);
        $brandUuid = $brandDictionary->getUuid($row->manufacturer);

        // set a key of either the brand name, or the uuid
        $key = $brandUuid ?  $brandUuid : $row->manufacturer;

        // instantiate the object for this brands results
        if( !array_key_exists($key, $resultsByBrand))
        {
            $resultsByBrand[$key] = new stdClass();
            $resultsByBrand[$key]->partnumber = $row->fullpartnumber;
            $resultsByBrand[$key]->brand = $row->manufacturer;
            $resultsByBrand[$key]->quantity = 0;
            $resultsByBrand[$key]->minimum_order_quantity = 1;
        }

        // include the qty with the total sum
        $resultsByBrand[$key]->quantity += $row->quantity;

        // make sure we need Product-level info before doing the work to get it
        if( !isset( $resultsByBrand[$key]->grams_piece_weight ))
        {

//echo '<pre>Brand UUID:<br />',print_r($brandUuid, TRUE),'</pre>';  // For Testing ----->
//echo '<pre>PN UUID:<br />',print_r($pnUuid, TRUE),'</pre>';  // For Testing ----->
//die('(Dead by request)');  // For Testing ----->
/**
 * This is a _**wierd**_ one ... 
 * An "Undefined Variable: $pnUuid" exception is getting thrown.
 * If we call "die()" or "continue()", it goes away.
 * Casting it as a global also works, but then it is NULL
 */
global $pnUuid;  // hackity hack hack hack

            // check for product-level info
            if( $brandUuid && $pnUuid )
            {
                $product = ProductEntity::where(
                                function ($query) use ($brandUuid, $pnUuid) {
                                    $query->where('partnumber_uuid', '=', $pnUuid);
                                    $query->where('company_uuid_brand', '=', $brandUuid);
                                })
                            ->first();

                // add product-level info to $resultsByBrand[$key]
                if( !empty( $product ))
                     $resultsByBrand[$key]->grams_piece_weight = $product->piece_weight;

            }   //if( $brandUuid && $pnUuid...
        }   //if( !property_exists...

        unset( $brandUuid, $pnUuid );

    }   //foreach( $rzStocklines AS...

    return $resultsByBrand;

}

这到底是怎么回事???

顺便说一句,我也只是尝试将它作为非静态方法,它仍然会引发相同的异常。

4

1 回答 1

7

它可能没有在第二次循环迭代中定义,因为您正在unset循环中设置变量...?!

于 2013-07-22T16:20:40.080 回答