PHP:这是“;未定义的变量“;异常是我的静态方法的范围问题


PHP: Is this "Undefined variable" exception a scope issue with my static method?

我有一个静态函数,它在进入foreach()循环之前定义了一些变量
foreach()中,我尝试调用我的$pnUuid var,并获得

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

如果我立即用die()continue执行打印,我可以成功地将前一个变量以及循环内的变量回显到循环中
调用global $pnUuid也可以绕过异常,但var为NULL:(

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;
}

这到底是怎么回事???

顺便说一句,我也只是将其作为一个非静态方法进行了尝试,但它仍然抛出了相同的异常。

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