duanqi6274 2015-09-23 13:11
浏览 20
已采纳

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

I got a bunch of classes and I want to send data from one class to another. But I am not able to achieve this. Hope you can help me with a solution for this.

I want to send data from this:

Report.php I have included the other file in the top of the file

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);
}

Then i want the data inside the DateReader, but i get nothing? Notice i try to echo it to see if it recieves the data:

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; 
    }
}

Hope you cant help me getting data from Report.php into DataReader.pgp.

  • 写回答

3条回答 默认 最新

  • dosin84644 2015-09-23 13:29
    关注

    As a simple example, remember that you can pass instances of classes as parameters to constructors, is the most common thing.

    This is just an example and adapt it to your code.

    <?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'
    

    In you case, although I don't know the rest of the code and it might not work, I hope you undertood the idea exposed above.

    EDIT: Fixed some logic problems, check DataReader class code again

    Constructor of the class Report:

    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);
    }
    

    And then

    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>";
        }
    }
    

    Now, let's say you create a new Report and the $_POST['pages'] has the value of 55

    $report = new Report();
    

    It will echo: 55;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容