0

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

$pagination = Pagination::getPagination(); This also returns the same error.
Yes, of course, because the double colon operator, is a token that allows access to static, constant, and overridden properties or methods of a class. Your class is not static and $limit variable is not constant, so you can't use this construction. PHP doc
Then what is the solution to access private variable from different functions while call from controller with its object ?
The best solution is the create new function in your class Pagination, which will return only the $limit variable - generatePagination() in my code do this. Then you need to create a new instance of your class Pagination and then you can get $limit value via ->generatePagination() call.
2

Shouldn't your keyword __constructor be __construct instead according to this link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.