Mixing Procedural code with OOP code — A PHP Conundrum

Thanks for all your helpful input, guys! But I’m still a bit lost. So here’s a more specific example, and maybe you can tell me how you’d approach this in OOP.

This is a simplified version of a class I use. It simply loads News content for a client website into an array so it can be passed to a template to generate an HTML page that shows a list of news articles.

class ClientNews 
{
	protected $db;
	protected $site;
	protected $widget;
	protected $client_id = 0;

	public function __construct(DB $db, Site $site, Widget $widget=null)
	{
		$this->db = $db;
		$this->site = $site;
		$this->widget = $widget;
	}

	public function setClientId($client_id)
	{
		if (!is_numeric($client_id)) {
			// If alpha ID passed, look up numeric ID for client
			$result = $this->db->select("SELECT id FROM clients WHERE client.alphaid = '$client_id' LIMIT 1");
			$this->client_id = $result['id'];
		} else {
			$this->client_id = $client_id;
		}
	}

	public function getNews() 
	{
		// This is where a query is built using the client_id in a WHERE statement if one specified. 
		// The query is also built using info from the $site and $widget objects passed in.

		$query = "SELECT ....";

		$records = $this->db->select($query);

		return $records; 
	}
}

The method setClientId() is used in a bunch of other classes that load various types of other content for a client website. Right now I’ve simply duplicated this method in each class that needs it, but that doesn’t seem right. Previously I would have made setClientId() a function in a global function library that would be included in every script. Easy! But I have no idea how this should be handled in OOP.

My first thought is that I should create a new class called ClientID.php and put the small snippet of code in there. But then that would mean I’d have to pass that into the ClientNews class, which means it would be a 4th dependency: $db (my Database class), $site (contains info about the client website), $widget (contains settings for the content widget, in this case News), and then $clientid.

But it won’t end there, if I do this with other functions I’m currently using from my procedural function library I could potentially need to pass a few more dependencies into this object to make it work. This seems a bit ridiculous to me. So I’m hoping this is not the right OOP solution! :slight_smile:

How would you handle this in OOP?