PHP:这个“未定义变量”异常是我的静态方法的作用域问题吗?
我有一个静态函数,它在进入foreach循环之前定义了几个变量。 在foreach中,我尝试调用$pnUuid var,并获取 未处理的异常未定义变量:pnUuid 我可以在循环之前成功地回显var,如果我立即使用die或continue跟踪打印,也可以从循环内部回显var。 调用全局$pnUuid也可以避免异常,但是var为NULL: 这到底是怎么回事PHP:这个“未定义变量”异常是我的静态方法的作用域问题吗?,php,scope,static-methods,Php,Scope,Static Methods,我有一个静态函数,它在进入foreach循环之前定义了几个变量。 在foreach中,我尝试调用$pnUuid var,并获取 未处理的异常未定义变量:pnUuid 我可以在循环之前成功地回显var,如果我立即使用die或continue跟踪打印,也可以从循环内部回显var。 调用全局$pnUuid也可以避免异常,但是var为NULL: 这到底是怎么回事 顺便说一句,我也只是尝试将其作为一个非静态方法,但它仍然引发相同的异常。它可能没有在第二次循环迭代中定义,因为您正在取消循环中的变量 异常被抛
顺便说一句,我也只是尝试将其作为一个非静态方法,但它仍然引发相同的异常。它可能没有在第二次循环迭代中定义,因为您正在取消循环中的变量 异常被抛出的确切位置是哪里?我在猜ProductEntity::where部分?哦,是的。。。知道这些可能会有帮助,嗯如果$brandUuid&$pnUuid。。。就在ProductEntity::where之前
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;
}