Data Collection Object

Basically I have no idea, what is the best way to collect/inject a bunch of data in an object which has clean code . let’s have some examples as below :

  1. we have an object to replace place holders with some variables, usually, we need to inject an array of pair key-value to this object. what is the standard way of collecting data for the object ??? do we need a global array and add or replace key-value data or need a JSON-YAML file? (how about changing placeholder values in different pages or controllers?) I really confuse some times what method is standards for these dynamic values which we don’t need to store it in DB. is any name for this kind of collecting data in PHP frameworks?
  2. we need some settings for an object, like API or bank gateway. how usually we inject it as we know it can be changed in different situations?

Well, an object can be changed. So create or change the object however you want and reuse it or create a new object to do a new task.

<?php

class Foo
{
	public $key;
	public $_url;
	
	public function __construct($api_key, $url)
	{
		$this->key = $api_key;
		$this->url = $url;
	}
}

class Request
{
	public static function createRequest(Foo $foo)
	{
		echo "Making request to {$foo->url} with api key {$foo->key}";
	}
}
$obj = new Foo('asdafaf4234e2f', 'google.com/s=something');
Request::createRequest($obj);
Sponsor our Newsletter | Privacy Policy | Terms of Service