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.
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;
Where is you setPageId function in DataReader. If you don't have it it would look like this:
public function setPageId($pageId) {
$this->pageId = $pageId;
}
also you couldn't do an echo in the constructor method because you calling the constructor and then calling the method setPageId after. The pageId would not have been set yet. Try echoing it out in the setPageId method.
The problem is that when you run this: $this->dataReader = new DataReader();
PHP will look at Datareader class for a construct, in that construct method, you have a line that needs this variable: $this->pageId
, however at this point, that variable is blank because it has not yet been declared.
To fix this, you need to pass the variable when you instantiate the object. In Reader.php
, rewrite the code like this:
public function __construct(){
// other code
$this->dataReader = new DataReader($this->pageId);
}
Then in Datareader.php
:
public function __construct($pageId) {
$this->setPageId($pageId);
//other code
}
Hope this helps.