为什么数组在PHP中为NULL?

时间:2022-06-06 21:30:55
<?php    

class ffooo
{
    public $arr;

    function __construct()
    {
        $arr=array();
    }

    function add($val)
    {
        $arr[]=$val;
    }

    function get($ind)
    {
        return $arr[$ind];
    }
}

$cont=new ffooo();
$cont->add("derek",'chmo');
echo $cont->get(0);
var_dump($cont);

Can anybody explain me why my array $arr is NULL after method add($val)? I try to echo array $arr in method "add",and in this method $arr contained come value; but in another method it becomed NULL? What is the magic?I do't understand the logic(

任何人都可以解释为什么我的数组$ arr在方法add($ val)之后是NULL?我尝试在方法“add”中回显数组$ arr,并在此方法中$ arr包含值;但在另一种方法中,它变成了NULL?什么是魔术?我不明白逻辑(

5 个解决方案

#1


1  

Because it is defined locally only. To use the class member, you must use $this;

因为它仅在本地定义。要使用类成员,必须使用$ this;

$this->arr

#2


1  

That's because you declare a variable $arr every time. And in every method it's just new, as functions have their own scope.

那是因为你每次都声明一个变量$ arr。在每种方法中它都是新的,因为函数有自己的范围。

You need to set a property, like that: $this->arr = array(...);. Properties exist in object scope, so they are accessible from every method.

你需要设置一个属性,如:$ this-> arr = array(...);.属性存在于对象范围中,因此可以从每个方法访问它们。

#3


0  

You declared the function with one argument:

您使用一个参数声明了该函数:

function add($val) {}

But pass to it two ones. And why do you use local copies of the class property in every function? Correct your code before talking about magic :)

但传递给它两个。为什么在每个函数中都使用class属性的本地副本?在谈论魔术之前纠正你的代码:)

#4


0  

Use $this->arr instead of $arr in all methods' bodies.

在所有方法的主体中使用$ this-> arr而不是$ arr。

#5


0  

You forgot to use $this, please see the code below.

您忘了使用$ this,请参阅下面的代码。

<?php    

class ffooo
{
    public $arr;

    function __construct()
    {
        $this->arr = array();
    }

    function add($val)
    {
        $this->arr[] = $val;
    }

    function get($ind)
    {
        return $this->arr[$ind];
    }
}

$cont=new ffooo();
$cont->add('derek');
echo $cont->get(0);
var_dump($cont);

#1


1  

Because it is defined locally only. To use the class member, you must use $this;

因为它仅在本地定义。要使用类成员,必须使用$ this;

$this->arr

#2


1  

That's because you declare a variable $arr every time. And in every method it's just new, as functions have their own scope.

那是因为你每次都声明一个变量$ arr。在每种方法中它都是新的,因为函数有自己的范围。

You need to set a property, like that: $this->arr = array(...);. Properties exist in object scope, so they are accessible from every method.

你需要设置一个属性,如:$ this-> arr = array(...);.属性存在于对象范围中,因此可以从每个方法访问它们。

#3


0  

You declared the function with one argument:

您使用一个参数声明了该函数:

function add($val) {}

But pass to it two ones. And why do you use local copies of the class property in every function? Correct your code before talking about magic :)

但传递给它两个。为什么在每个函数中都使用class属性的本地副本?在谈论魔术之前纠正你的代码:)

#4


0  

Use $this->arr instead of $arr in all methods' bodies.

在所有方法的主体中使用$ this-> arr而不是$ arr。

#5


0  

You forgot to use $this, please see the code below.

您忘了使用$ this,请参阅下面的代码。

<?php    

class ffooo
{
    public $arr;

    function __construct()
    {
        $this->arr = array();
    }

    function add($val)
    {
        $this->arr[] = $val;
    }

    function get($ind)
    {
        return $this->arr[$ind];
    }
}

$cont=new ffooo();
$cont->add('derek');
echo $cont->get(0);
var_dump($cont);