为什么我不能将数据从一个类发送到另一个类?

时间:2015-09-23 13:11:46

标签: php oop

我有一堆类,我想将数据从一个类发送到另一个类。但我无法做到这一点。希望你能帮我解决这个问题。

我想从此发送数据:

Report.php 我已将其他文件包含在文件顶部

include('DataReader.php');

public function __construct(){
    $this->pageId = $_POST['pages'];
    $this->since = strtotime($_POST['sincedate']);
    $this->until = strtotime($_POST['untildate']);
    echo "Report: " . $this->pageId . "<br>";
    $this->dataReader = new DataReader();
    $this->dataReader->setPageId($this->pageId);
}

然后我想要DateReader中的数据,但我什么都没得到?请注意我尝试回应它以查看它是否收到了数据:

DataReader.php

class DataReader {
    private $pageId;
    public $since;
    public $until;
    public $accessToken;
    public $fb;
    /**
     * @var FacebookRest
     */
    private $facebook;

    // Start app with app details from facebook
    public function __construct() {
        echo "DataReader: " . $this->pageId . "<br>";
        $this->facebook = FacebookRest::getInstance();
        $this->facebook->setPageId($this->pageId);
        $this->facebook->setSince($this->since);
        $this->facebook->setUntil($this->until);
    }

    public function setPageId($pageId) { 
        $this->pageId = $pageId; 
        echo $this->pageId; 
    }
}

希望您无法帮助我将Report.php中的数据导入DataReader.pgp。

3 个答案:

答案 0 :(得分:3)

作为一个简单的例子,请记住,您可以将类的实例作为参数传递给构造函数,这是最常见的事情。

这只是一个示例,并使其适应您的代码。

<?php

class Foo
{
    public $name;
    public $bar;

    public function __construct()
    {
        $this->name = "John Doe";
        $this->bar = new Bar($this);
    }
}

class Bar
{
    public function __construct(Foo $foo)
    {
        echo $foo->name;
    }
}

$foo = new Foo(); // Echoes 'John Doe'

在你的情况下,虽然我不知道代码的其余部分并且它可能不起作用,但我希望你能够实现上面提到的想法。

  

编辑:修复了一些逻辑问题,再次检查DataReader类代码

类报告的构造函数:

public function __construct()
{
    $this->pageId = $_POST['pages'];
    $this->since = strtotime($_POST['sincedate']);
    $this->until = strtotime($_POST['untildate']);
    echo "Report: " . $this->pageId . "<br>";
    $this->dataReader = new DataReader($this);
}

然后

class DataReader
{
    private $pageId;
    public $since;
    public $until;
    public $accessToken;
    public $fb;
    /**
    * @var FacebookRest
    */
    private $facebook;

    // Start app with app details from facebook
    public function __construct(Report $report)
    {
        echo "Report Page ID: " . $report->pageId . "<br>";
    }
}

现在,我们假设您创建了一个新报告,而$_POST['pages']的值为55

$report = new Report();

它将回应:55;

答案 1 :(得分:0)

DataReader中的setPageId函数在哪里。如果你没有它,它将如下所示:

public function setPageId($pageId) {
    $this->pageId = $pageId;
}

你也不能在构造函数方法中做一个echo,因为你调用构造函数然后调用方法setPageId。 pageId尚未设置。尝试在setPageId方法中回显它。

答案 2 :(得分:0)

问题是当你运行它时:$this->dataReader = new DataReader(); PHP会查看构造的Datareader类,在该构造方法中,你有一行需要这个变量:$this->pageId,但是在这个point,该变量为空,因为尚未声明。

要解决此问题,您需要在实例化对象时传递变量。在Reader.php中,重写代码如下:

public function __construct(){
    // other code
    $this->dataReader = new DataReader($this->pageId);
}

然后在Datareader.php

public function __construct($pageId) {
    $this->setPageId($pageId);
    //other code
}

希望这有帮助。

相关问题