Reputation: 1795
I try to get value of a private variable (limit), but I get the following error:
Fatal error: Uncaught Error: Using $this when not in object context in /home/vagrant/Code/wp/wp-content/plugins/StorePress/app/library/Pagination.php on line 36
My Class:
class Pagination
{
private $limit = 0;
private $limit_start = 0;
private $total = 0;
/**
* Generate Pagination for Products
* @param $pagination
* @return string
*/
public function __constructor($pagination = null)
{
$this->limit = $pagination['limit'];
$this->lim_start = ($pagination['start']) ?: null;
$this->total = $pagination['total'];
}
public function generatePagination()
{
echo $this->limit;
}
Here, I'm trying to print "$this->limit", a private variable, but it's not allowed to print the value that are assigned by the "__constructor".
Is there anything wrong in my code or is there any other solution to get that value?
Upvotes: 0
Views: 99
Reputation: 17354
Shouldn't your keyword __constructor be __construct instead according to this link
Upvotes: 2
Reputation: 1587
I think, that the problem is in your OOP construction. You cannot echo $this
private variable, when you don't create class object as first. So the solution might be:
class Pagination
{
private $limit = 0;
private $limit_start = 0;
private $total = 0;
/**
* Generate Pagination for Products
* @param $pagination
* @return string
*/
public function __constructor($pagination = null)
{
$this->limit = $pagination['limit'];
$this->lim_start = ($pagination['start']) ?: null;
$this->total = $pagination['total'];
}
public function generatePagination()
{
return $this->limit;
}
and then in your code, where you need to echo the limit value, you can use:
$pagination = new Pagination();
echo $pagination->generatePagination();
At first line, you will create new Pagination() object and in the second line, you will return the $limit value from your generatePagination class function.
Upvotes: 3