dspym82000 2017-04-24 15:31 采纳率: 100%
浏览 65
已采纳

一旦函数调用发生,PHP类__Constructor参数为NULL

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条回答 默认 最新

  • dongye9191 2017-04-24 16:02
    关注

    You can pass multiple parameters to constructor the way i am doing in my post. But if Lets say you have 10 parameters to pass to constructor so might be you dont want to write it like public function __construct($a,$b,$c,$d,$e....) which is not a good approach in itself.

    Try this code snippet here

    <?php
    
    ini_set('display_errors', 1);
    class MyClass
    {
        protected $a=0;
        protected $b=0;
        protected $c=0;
        protected $d=0;
        protected $e=0;
    
        public function __construct(array $options=array())
        {
            foreach($options as $key => $value)
            {
                $this->{$key}=$value;
            }
            print_r($this);
        }
    }
    
    $object = new MyClass(array(
        'a'=>1,
        'b'=>2,
        'c'=>3,
        'd'=>4,
        'e'=>5,
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因