0
votes

After all, searching enough in SO I couldn't find out a most generic with best practice solution for PHP __constructor with multiple parameter into a class function

I am trying to define a function inside a PHP class. Where I will be using this function multiple times through a simple function call. Where the function will be having 2 or more parameters.

When I do a function call by passing the parameter, it's just NULL when it reaches the __constructor.

  1. Why it's NULL?

Also, note that there are objects nested inside the function addFruitCheckBox.

  1. What I am doing wrong?

  2. I may also wanted to pass a function call instead of $this->addFruitCheckBoxItemName sometimes.

There are lot of specific problems and solutions in SO. However, I believe this generic question will help me and all, for passing mulitple parameter into __constructor function in a PHP class


ini_set('display_errors', 1);

$_GET['SELECTEDTABNAME'] = 'properties';

/* all include files that are involved in function call within a function will be declared here */

class AddFruitController
{

    protected $addFruitCheckBoxItemName;
    protected $addFruitCheckBoxLabel;
    protected $addFruitMenuItemName;
    protected $addFruitChoiceItemName;
    protected $addFruitTimeItemName;
    public $trustedFruits;
    public $trustedFruitsModel;
    public $trustedFruitsSpeed;
    public $addNewFruit;
    public $additionalTub;
    public $chooseParent;
    public $FruitDown;
    public $FruitSell;
    public $timeTitle;
    public $addFruitbutton;

    public function __construct($addFruitCheckBoxItemName, $addFruitCheckBoxLabel, $addFruitMenuItemName, $addFruitTimeItemName)
    {
        global $interpreterMan, $fetchSeedForSapling;

        // var_dump($this->addFruitCheckBoxLabel);
        // var_dump($this->addFruitCheckBoxItemName);

        $this->trustedFruits = $interpreterMan("Trusted Fruits");
        $this->trustedFruitsModel = $interpreterMan("Model");
        $this->trustedFruitsSpeed = $interpreterMan("Speed");
        $this->addNewFruit = $interpreterMan("New Fruit");
        $this->additionalTub = $interpreterMan("Additional Options");
        $this->chooseParent = $interpreterMan("Choose Parent");
        $this->FruitDown = $interpreterMan("Download Schedule");
        $this->FruitSell = $interpreterMan("Install Schedule");
        $this->timeTitle = $interpreterMan("Time");
        $this->addFruitbutton = $interpreterMan("Add Fruit(s)");

        $this->addFruitCheckBoxItemName = $addFruitCheckBoxItemName;
        $this->addFruitCheckBoxLabel = $addFruitCheckBoxLabel;
        $this->addFruitMenuItemName = $addFruitMenuItemName;
        $this->addFruitChoiceItemName = $addFruitChoiceItemName;
        var_dump($addFruitChoiceItemName);
        $this->addFruitTimeItemName = $addFruitTimeItemName;

    }

    public function addFruitMenu()
    {
        global $interpreterMan;

        $theWidfetch = new FruitMenu();
        $theWidfetch->AssignAddJsCode(false);
        $theWidfetch->AssignChoiceOrder(array($interpreterMan("English")));
        $theWidfetch->AssignChoiceText(array($interpreterMan("English") => $interpreterMan("English")));
        $theWidfetch->AssignGroupHeader($this->addFruitMenuItemName);
        $theWidfetch->AssignItemName($this->addFruitMenuItemName);
        $theWidfetch->AssignSaveLocation($this->addFruitMenuItemName);
        $theWidfetch->AssignValueToUse("ipad");
        $theWidfetch->WaterPath(true, true);
    }

    public function addFruitChoiceTable()
    {
        global $fetchSeedForSapling, $interpreterMan;
        $weekChoiceSelection = new FruitChoiceTable();
        $weekChoiceSelection->AssignAddJsCode(false);
        $weekChoiceSelection->AssignChoiceOrder(
            array("sun", "mon", "tue", "wed", "thu", "fri", "sat"));
        $weekChoiceSelection->AssignChoiceText(array(
            "sun" => $interpreterMan("SUN"),
            "mon" => $interpreterMan("MON"),
            "tue" => $interpreterMan("TUE"),
            "wed" => $interpreterMan("WED"),
            "thu" => $interpreterMan("THU"),
            "fri" => $interpreterMan("FRI"),
            "sat" => $interpreterMan("SAT"),
        ));
        var_dump($weekChoiceSelection->AssignGroupHeader($this->addFruitChoiceItemName));
        $weekChoiceSelection->AssignItemName("Weekday");
        $weekChoiceSelection->AssignNumColumns(7);
        $weekChoiceSelection->AssignValueToUse($fetchSeedForSapling("dayOfWeek"));
        $weekChoiceSelection->WaterPath(true, true);
    }

    public function addFruitTime()
    {
        global $fetchSeedForSapling;
        $FruitTimeSelect = new FruitTime();
        $FruitTimeSelect->AssignGroupHeader($addFruitTimeItemName);
        $FruitTimeSelect->AssignItemName($addFruitTimeItemName);
        $FruitTimeSelect->AssignValueToUse($fetchSeedForSapling("minuteOfDay"));
        $FruitTimeSelect->WaterPath(true, true);
    }

    public function addFruitCheckBox()
    {
        global $fetchSeedForSapling;
        $addFruitCheckBoxObj = new FruitCheckbox();
        $addFruitCheckBoxObj->AssignAddJsCode(false);
        $addFruitCheckBoxObj->AssignCheckboxLabel($this->addFruitCheckBoxLabel);
        $addFruitCheckBoxObj->AssignItemName($this->addFruitCheckBoxItemName);
        $addFruitCheckBoxObj->AssignSaveLocation("somejob");
        $addFruitCheckBoxObj->AssignValueToUse($fetchSeedForSapling("somejob"));
        $addFruitCheckBoxObj->WaterPath(true, true);
    }
}
2
That's not even valid PHP.Jonnix
I can show you reference in same SO, where they have followed the method and has got upvotes too. For passing parameters into class name. @JonStirlingCommon Man
You can pass as many arguments as you want into the constructor, but if the constructor doesn't do anything with them, nothing magical is going to happenMark Baker

2 Answers

2
votes

For creating such complex objects, I suggest you to use Builder Design Pattern instead of assigning properties dynamically and directly.

Note: For better, you can add a layer of interface which Builder classes will implement. And you can have multiple Builder classes which generate different complex objects as per different use cases. Hope this make sense.

Try this code snippet here

<?php

class Builder {
    public static function getMyClass($a, $b, $c) {
        $myClass = MyClass::getInstance();
        $myClass->setA($a);
        $myClass->setB($b);
        return $myClass;
    }
}
class MyClass {
    protected $a=0;
    protected $b=0;

    public static function getInstance() {
        $myClass = new MyClass();
        return $myClass;
    }

    function setA($a) {
        $this->a = $a;
    }

    function setB($b) {
        $this->b = $b;
    }
}

$myClass = Builder::getMyClass("a", "b", "c");
print_r($myClass);

Explanation: In the above mentioned code we have a Builder class which is responsible for building such complex objects.

But still if you are still more towards dynamic assignment approach which nobody recommends, you can see this post

2
votes

http://php.net/manual/en/language.oop5.decon.php

class MyClass {
    protected $a1;
    protected $a2;
    protected $a3;

    public function __construct($a1, $a2, $a3) {
        $this->a1 = $a1;
        $this->a2 = $a2;
        $this->a3 = $a3;
    }
}