全局变量在php中不返回值
我已经在类外定义了一个变量。我通过关键字全局变量在php中不返回值,php,class,function,global-variables,return-value,Php,Class,Function,Global Variables,Return Value,我已经在类外定义了一个变量。我通过关键字global在类的函数中访问它。我在这个变量中设置了一些值并返回变量。但是我调用函数的地方什么也得不到。这是我的密码 $category_ids=array(); class Categoriesinfo { function get_categories_info($feed_id) { $all_categories[] = $this->get_parent_ids(5); //here I receive only
global
在类的函数中访问它。我在这个变量中设置了一些值并返回变量。但是我调用函数的地方什么也得不到。这是我的密码
$category_ids=array();
class Categoriesinfo
{
function get_categories_info($feed_id)
{
$all_categories[] = $this->get_parent_ids(5);
//here I receive only null
echo '<pre>';
var_dump($all_categories);
echo '</pre>';
}
function get_parent_ids($category_id)
{
global $db;
//echo '<br> '.$category_id;
global $category_ids;
$parent_id = $db->Execute("SELECT categories_id,parent_id FROM ".DB_PREFIX."_categories WHERE categories_id='$category_id'");
if($parent_id->fields['parent_id']==0)
{
$category_ids[]=$parent_id->fields['categories_id'];
//when I take print_r of $category_ids it shows my values but where I call this function it shows null
return $category_ids;
}
else
{
$category_ids[]=$parent_id->fields['categories_id'];
$this->get_parent_ids($parent_id->fields['parent_id']);
}
}
}
$category_id=array();
类别信息
{
函数获取类别信息($feed\u id)
{
$all_categories[]=$this->get_parent_id(5);
//这里我只收到空的
回声';
var_转储(所有类别);
回声';
}
函数get\u parent\u id($category\u id)
{
全球$db;
//回显“
”.$category\u id;
全球$category_id;
$parent\u id=$db->Execute(“从“.db\u前缀”中选择类别\u id,parent\u id。”“类别中的类别\u id='$category\u id'”);
如果($parent\u id->fields['parent\u id']==0)
{
$category\u id[]=$parent\u id->fields['categories\u id'];
//当我获取$category\u id的print\r时,它会显示我的值,但在我调用此函数的地方,它会显示null
返回$category_id;
}
其他的
{
$category\u id[]=$parent\u id->fields['categories\u id'];
$this->get_parent_id($parent_id->fields['parent_id');
}
}
}
现在在get\u parent\u id
函数中,当我获取global
变量的print\r
时,我可以看到一个包含两个值的数组。但是当我调用这个函数时,我只收到null。未返回此变量。我的代码有问题吗,或者我做错了什么
注意
实际上,我正在循环中调用函数
get\u parent\u ids()
。我没有在代码中提到它。所以每次变量的值都会改变。因此,请考虑这个因素,当你是全局时,为什么要返回<代码> $CyryYyIDS < /C>?全局是双向的,我不明白。实际上,我正在循环中调用函数get\u parent\u id
。我没有在代码中提到它。所以每次变量的值都会改变。因此,现在如何做/无论什么代码调用您的get\u parent\u ids()
方法都会在方法调用完成后引用全局$category\u ids
,就像它是从函数返回的一样(它不应该是)。@Crontab现在如何重置该变量值?当它在循环中被调用时,它也具有先前的值。循环的新迭代之前也设置了这个变量的值?像其他变量一样重置它;将其设置为array()
或null
或”
或0
或任何您喜欢的设置。