$this PHP returns null
Just wondering why the get method returns Null
class Test
{
protected $title;
public function setTitle($ttl)
{
$this->title = $ttl;
}
public function getTitle()
{
var_dump($this->title); return Null ???
return (string) $this->title;
}
Calling the methods
in Controller 1
$test = new Test();
$test->setTitle('ttl');
get Method in Controller 2
$test->getTitle(); return Null
Answer
Solution:
If you want to use the getTitle();
in another controller, then you need to export your object from the first controller. Because that's how OOP works.
If you create in a file something like this:
$title= new Title();
$title->setTitle('tlt');
And you go in another file(controller) and write this:
$title = new Title();
$title->getTitle('tlt');
You'll get NULL
. This is because the $title
from the first Controller isn't the same object with the $title
from the second controller.
Try to use a function to export your object: try this in the first controller
function exports(){
$title= new Title();
$title->setTitle('tlt');
return $title
}
And this in the second:
$title = exports();
$title->getTitle();
Source