__construct() public method

Constructor Adds config values to the public properties when a new object is created.
public __construct ( array $config = [] ) : void
$config array Available configuration options are: - `'scheme'` _string_: 'tcp' - `'host'` _string_: 'localhost' - `'port'` _integer_: null - `'username'` _string_: null - `'password'` _string_: null - `'path'` _string_: null - `'body'` _mixed_: null
return void
Beispiel #1
0
 /**
  * Adds config values to the public properties when a new object is created.
  *
  * @param array $config Configuration options : default value
  * - `scheme`: http
  * - `host`: localhost
  * - `port`: null
  * - `username`: null
  * - `password`: null
  * - `path`: null
  * - `version`: 1.1
  * - `headers`: array
  * - `body`: null
  */
 public function __construct(array $config = array())
 {
     $defaults = array('scheme' => 'http', 'host' => 'localhost', 'port' => null, 'username' => null, 'password' => null, 'path' => null, 'protocol' => null, 'version' => '1.1', 'headers' => array(), 'body' => null);
     $config += $defaults;
     parent::__construct($config);
     if (strpos($this->host, '/') !== false) {
         list($this->host, $this->path) = explode('/', $this->host, 2);
     }
     $this->path = str_replace('//', '/', "/{$this->path}");
     $this->protocol = $this->protocol ?: "HTTP/{$this->version}";
 }
Beispiel #2
0
 /**
  * Adds config values to the public properties when a new object is created.
  *
  * @param array $config Configuration options : default value
  *        - `'protocol'` _string_: null
  *        - `'version'` _string_: '1.1'
  *        - `'scheme'` _string_: 'http'
  *        - `'host'` _string_: 'localhost'
  *        - `'port'` _integer_: null
  *        - `'username'` _string_: null
  *        - `'password'` _string_: null
  *        - `'path'` _string_: null
  *        - `'headers'` _array_: array()
  *        - `'body'` _mixed_: null
  */
 public function __construct(array $config = array())
 {
     $defaults = array('protocol' => null, 'version' => '1.1', 'scheme' => 'http', 'host' => 'localhost', 'headers' => array());
     $config += $defaults;
     foreach (array_intersect_key(array_filter($config), $defaults) as $key => $value) {
         $this->{$key} = $value;
     }
     parent::__construct($config);
     if (strpos($this->host, '/') !== false) {
         list($this->host, $this->path) = explode('/', $this->host, 2);
     }
     $this->path = str_replace('//', '/', "/{$this->path}");
     $this->protocol = $this->protocol ?: "HTTP/{$this->version}";
 }