php OOP从函数返回

php OOP从函数返回,php,function,oop,return,Php,Function,Oop,Return,一些奇怪的问题,这是我的代码: 我必须回显$list,但现在它是空的。 “class GuestBook”从服务器读取文件,然后我向数组添加一些新信息,我想返回一个新数组(数组是$list),现在它显示$list为NULL <?php class GuestBook { public function __construct($path) { $this->path = $path; $this->list = $list; } public functi

一些奇怪的问题,这是我的代码: 我必须回显$list,但现在它是空的。 “class GuestBook”从服务器读取文件,然后我向数组添加一些新信息,我想返回一个新数组(数组是$list),现在它显示$list为NULL

<?php

class GuestBook {
public function __construct($path)
{
    $this->path = $path;
    $this->list = $list;

}

public function getData() {
    $list = file($this->path);
    return $this->list;
}

public function append($text)
{
    $list = file($this->path);
    $list[] = $text;
    var_dump($list);
    return $this->list;
}

public function getList()
{
    var_dump($list); // NULL

}

};

$newBook = new GuestBook(__DIR__ . '/HW_db.txt');
$newBook->getData();
$newBook->append('HI!');
$newBook->getList(); // NULL??


var_dump($newBook->list); // NULL ??

您使用的是局部变量,而不是变量。正确答案是:

public function getList()
{
    var_dump($this->list);
}

您使用的是局部变量,而不是。正确答案是:

public function getList()
{
    var_dump($this->list);
}

简言之,这将起作用

<?php

class GuestBook {

    public $path,$list;

    public function __construct($path){
        $this->path = $path;
    }
    public function getData() {
        if(file_exists($this->path)){
            $this->list = file($this->path);
            return $this;
        }
        throw new \Exception(" Your file path is invalid ");
        return null;
    }
    public function append($text){
        $this->list[] = $text;
        return $this;
    }
    public function getList(){
        return $this->list;
    }
}
//usage

$guestbook = new GuestBook(__DIR__ . '/HW_db.txt');
$guestbook->getData()->append("HI");

var_dump($guestbook->getList());
?>

它是如何工作的 好的,让我们从顶部开始;首先声明类名,然后设置变量。在本例中,您使用了
$path
$list
,为了简单起见,我们将它们设置为
public
。如果您想了解更多关于变量作用域的信息,请参阅

然后我们转到构造函数,设置传递给
public$path
变量的路径,以便在整个类中使用它

在函数中,我们检查文件是否存在。如果存在,我们将数据加载到
public$list
变量中。如果没有,我们抛出一个异常

为了附加到该列表中,我们只需添加到
public$list
的数组中

然后,为了获得整个列表,我们在
getList()
函数中返回$this->list


有了这些,你就有了一个运转良好的班级

简单地说,这将起作用

<?php

class GuestBook {

    public $path,$list;

    public function __construct($path){
        $this->path = $path;
    }
    public function getData() {
        if(file_exists($this->path)){
            $this->list = file($this->path);
            return $this;
        }
        throw new \Exception(" Your file path is invalid ");
        return null;
    }
    public function append($text){
        $this->list[] = $text;
        return $this;
    }
    public function getList(){
        return $this->list;
    }
}
//usage

$guestbook = new GuestBook(__DIR__ . '/HW_db.txt');
$guestbook->getData()->append("HI");

var_dump($guestbook->getList());
?>

它是如何工作的 好的,让我们从顶部开始;首先声明类名,然后设置变量。在本例中,您使用了
$path
$list
,为了简单起见,我们将它们设置为
public
。如果您想了解更多关于变量作用域的信息,请参阅

然后我们转到构造函数,设置传递给
public$path
变量的路径,以便在整个类中使用它

在函数中,我们检查文件是否存在。如果存在,我们将数据加载到
public$list
变量中。如果没有,我们抛出一个异常

为了附加到该列表中,我们只需添加到
public$list
的数组中

然后,为了获得整个列表,我们在
getList()
函数中返回$this->list

有了这些,你就有了一个运转良好的班级

你的代码有什么问题? 简言之:一切

如何修复它 让我们分析其中的每一部分,看看如何纠正和改进它

class GuestBook {
    public function __construct($path)
    {
        $this->path = $path;
        $this->list = $list;
    }
构造函数的第一条语句是正确的,它执行构造函数应该执行的操作:初始化类成员。对于代码的读者来说,最好在类定义的顶部声明
$path
$list
,如下所示:

class GuessBook {
    private $path;
    private $list;

    public function __construct($path)
    ...
但是,构造函数的第二条语句使用变量
$list
的值初始化对象的
list
属性(
$this->list
),该变量在函数中的任何地方都没有提到;它不存在,这意味着它未定义,其值为
NULL

您可以将
$list
作为构造函数的第二个参数(
公共函数\uu构造($path,$list)
)传递,也可以使用固定值初始化
$this->list
)。从您使用该类的方式来看,我认为第二个选项是要走的路,
$this->list
的最合适值是一个空的
array()


第一条语句加载文件内容,将其拆分为行,并将行数组存储到局部变量
$list
中。这里没什么问题。但是第二条语句返回当前对象的
list
属性的值,我们要记住,它是在构造函数中用
NULL
初始化的

我相信您的意图是将从文件加载的行数组存储在
列表
属性中。通过将第一行代码更改为:

$this->list = file($this->path);
在这里,方法的名称并不能清楚地表示它的功能。其主要目的是在
$this->list
中加载文件内容。更好的名称是
loadData()


首先,
var\u dump()。它是一个调试功能,将其放入代码中,使用它显示的数据,并在代码修复后立即将其从代码中删除,不再需要它的存在。我希望你把它放在这里发布的代码中,并从真正的代码中删除它

此方法的问题与
getData()
相同。它将数据加载到局部变量
$list
,该变量在函数完成后立即销毁。但是,使用
$this->list
而不是
$list
只能解决一半的问题

当您连续两次调用
append()
时会发生什么?第二次调用丢弃第一次调用准备的
$this->list
的内容。这(部分)是因为
append()
执行两件事:加载数据并追加新行。让
getData()
加载数据并将
append()
更改为仅追加一行新行:

public function append($text)
{
    $this->list[] = $text;
    return $this->list;
}

函数名(
getList()
)表示它返回一个值。相反,它显示了一些东西。这令人困惑。同样,这里没有放置
var\u dump()
的位置

但是这个函数的主要问题是它试图使用未初始化的局部变量
$list
。它大概应该是这样的:

public function getList()
{
    return $this->list;
}

不需要在类定义后加分号(
)。不过,这不是一个错误。这只是一个例子


这里没什么问题,只是出人意料。不知道类
GuestBook
的代码的读者需要
$newBook->getList()
返回一个值,而不是pr
public function getList()
{
    var_dump($list); // NULL
}
public function getList()
{
    return $this->list;
}
};
$newBook = new GuestBook(__DIR__ . '/HW_db.txt');
$newBook->getData();
$newBook->append('HI!');
$newBook->getList(); // NULL??
var_dump($newBook->list); // NULL ??
class GuestBook {
    private $path;
    private $list;

    public function __construct($path)
    {
        $this->path = $path;
        $this->list = array();
    }

    public function loadData()
    {
        $this->list = file($this->path);
        return $this->list;
    }

    public function append($text)
    {
        $this->list[] = $text;
        return $this->list;
    }

    public function getList()
    {
        return $this->list;
    }
}

$newBook = new GuestBook(__DIR__ . '/HW_db.txt');
$newBook->loadData();
$newBook->append('HI!');
print_r($newBook->getList());
class GuestBook {
    private $path;
    private $list;

    public function __construct($path)
    {
        $this->path = $path;
        $this->list = file($this->path);
    }

    public function append($text)
    {
        $this->list[] = $text;
        return $this->list;
    }

    public function getList()
    {
        return $this->list;
    }

    public function __destruct()
    {
        file_put_contents(this->file, implode('', $this->list));
    }
}

$newBook = new GuestBook(__DIR__ . '/HW_db.txt');
$newBook->append('HI!');
print_r($newBook->getList());
unset($newBook);              // This calls the destructor 
class GuestBook {
    private $path;
    private $list;

    public function __construct($path)
    {
        $this->path = $path;
        $this->list = explode("\n", file_get_contents($this->path));
    }

    public function append($text)
    {
        $this->list[] = trim(str_replace("\n", ' ', $text));
        return $this->list;
    }

    public function getList()
    {
        return $this->list;
    }

    public function __destruct()
    {
        file_put_contents(this->file, implode("\n", $this->list));
    }
}