1
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.

4
  • This question would probably be a better match for programmers.stackexchange.com Commented Mar 27, 2015 at 7:29
  • I do this all the time and I do it basically as your question states it. I set my class variables to the default, then set them to the passed values if not null. Commented Mar 27, 2015 at 7:41
  • 2
    @Havelock no it wouldn't, if the code works then it fits on codereview.SE (it has to actually work; they are very strict about that), this is otherwise a "best practice" question in disguise and will quickly get closed on programmers. Commented Mar 27, 2015 at 9:09
  • @ratchetfreak, absolutely right, I just couldn't remember what the correct site was and a quick skim read the list with SE sites didn't do it for me ;-) Commented Mar 27, 2015 at 10:06

3 Answers 3

1

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;

    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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'
));

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.