SIDDHARTH GOSWAMI
SIDDHARTH GOSWAMI

Reputation: 11

PHP setting optional arguments

class JSON_Response{
            private $ResponseCode = "RVN-775";
            private $ResponseDescription = "Unknown Request";
            private $ResponseOf = "Unknown";
            private $ResponsePayload = array("PayloadEmpty"->"Yes");

            function __construct(
                            $ResponseCode = null,
                            $ResponseDescription = null,
                            $ResponseOf = null,
                            $ResponsePayload = null,
                            )
        {
            $this->ResponseCode = $ResponseCode ? $ResponseCode : $this->ResponseCode;
            $this->ResponseDescription = $ResponseDescription ? $ResponseDescription : $this->ResponseDescription;
            $this->ResponseOf = $ResponseOf ? $ResponseOf : $this->ResponseOf;
            $this->ResponsePayload = $ResponsePayload ? $ResponsePayload : $this->ResponsePayload;

        }
    }

Is there any better way to write this?

I want the class variables to be set if constructor gets parameters when object is created, if no parameters are given then I want the class variables to be default.

Upvotes: 1

Views: 49

Answers (3)

Alexis Peters
Alexis Peters

Reputation: 1631

This Will Override the Defaults, if some arguments are passed:

class JSON_Response{
      function __construct(
                        $ResponseCode = "RVN-775",
                        $ResponseDescription = "Unknown Request",
                        $ResponseOf = "Unknown",
                        $ResponsePayload =  array("PayloadEmpty"->"Yes"),
                        )
    {
        $this->ResponseCode = $ResponseCode;
        $this->ResponseDescription = $ResponseDescription;
        $this->ResponseOf = $ResponseOf;
        $this->ResponsePayload = $ResponsePayload;

    }
}

Upvotes: 1

Girish
Girish

Reputation: 12127

You can use all __constructor() in array and assign value in class variable if params array exist class variable key, this would reduce your code size and better to use it, see below sample code

<?php

class JSON_Response {

    private $ResponseCode = "RVN-775";
    private $ResponseDescription = "Unknown Request";
    private $ResponseOf = "Unknown";
    private $ResponsePayload = array("PayloadEmpty" => "Yes");

    function __construct($params = array()) {
        if(!empty($params)){
            foreach ($params as $k => $v){
                $this->{$k} = $v;
            }
        }
    }
}

#call class from with array params
$class_params = array('ResponseCode' => '', 'ResponseDescription' => '',
 'ResponseOf' => '', 'ResponsePayload' => '');
$obj = new JSON_Response($class_params);

if any class variable does not exist in params key then class variable default value will use

Upvotes: 0

darioguarascio
darioguarascio

Reputation: 1087

you could try passing an array:

function __construct($args) { 
   foreach ($args as $k => $v) {
       //if (property_exists($this,$k)) -- optionally you want to set only defined properties
       $this->{$k} = $v;
   }
}

and then create the object using:

$object = new JSON_Response(array(
    'ResponseOf' => 'test'
));

Upvotes: 0

Related Questions