Reuse class throughout my site? Please help

Hi Folks,

I’m trying to use a class to render partials throughout my site:

I have this main class, payment.php

class Payment {
	
	private function  GetUserPayments($isPaid = false)
	{
	//eloquent stuff

	return $results
	}

}

I would like to reuse this throughout my site in my controller, something like this:

public function getPendingPayments()
	{	

		$payments = $this->Payments::GetUserPayments(true);


		return View::make(
			'layouts.parents.pending-payments',
			array('payments' => $payments)  );

	}

I am getting an error: PHP Parse error: syntax error, unexpected ‘::’.

Can anyone please pointme in the right direction, how could I resuse this class throughout my controller?

Many thanks

EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.

You’re receiving the error because you are attempting to use the scope resolution operator (::slight_smile: to perform a scoped call to a non-static method (that’s privately set anyway).

If you’d like to invoke the GetUserPayments() method, then define it as a public static method:

public static function  GetUserPayments($isPaid = false)
{
	//eloquent stuff

	return $results
}

With that said, defining things statically unnecessarily can quickly make your code base quite a mess. In your scenario, I’d personally use dependency injection where the Payment class (a model object?) is simply passed into the constructor of the controller and assigned to an instance variable. That way, it can be used by other methods of your controller classes without having scoped calls firing off everywhere.

Maybe this would be a good case to use an abstract class?

Thanks very much for the replies, I’ll let you know how I get on.

Well your class is designed properly, but not used appropriately. Also learn how to use dependency injection and do not rely on static accesses, since statics are bad OOP practice and really aint about reusability.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.