Does the optional parameter in PHP have to be followed by the required parameter?

Source: Internet
Author: User
{Code...} is always mandatory Parameter definition in my image before the optional parameter definition. The above code shows the source code in lotusphp. How can I use this definition?
public function addHost($groupId, $nodeId = "node_0", $role = "master", $hostConfig){    //.....}

In my image, mandatory parameters are always defined before the optional parameters. The above code shows the source code in lotusphp. How can I use this definition?

Reply content:
public function addHost($groupId, $nodeId = "node_0", $role = "master", $hostConfig){    //.....}

In my image, mandatory parameters are always defined before the optional parameters. The above code shows the source code in lotusphp. How can I use this definition?

Function test ($ a, $ B = 3, $ c = 4, $ d) {echo $. $ B. $ c. $ d;} echo test (4, "", "", 9); // print the result // 49 echo test (4, null, null, 9 ); // print the result // 49

In fact, the default values of $ B = 3 and $ c = 4 will never be set successfully.
If the fourth parameter is a required parameter, the function must have more than four parameters. The default parameter is meaningless.

1. Your impression is incorrect. The required parameters and optional parameters are not sequential. You do not have to put the optional parameters at the end.
2. The addHost () method of lotusphp is designed as follows mainly because the group, node, and role are progressive at the conceptual level, and the reverse order logic is not conducive to memory, while the config
3. the call method is addHost ("global_group", array ("host" => "127.0.0.1"). For more call methods, see lotusphp/unittest/Db/RightWayToUse. php

There is a positive solution for upstairs. in php, the location of default parameters is not emphasized, but cannot be separated during the call, as shown in
Function test ($ a, $ B = 3, $ c = 4, $ d)
{
Echo $ a. $ B. $ c. $ d;
}
Echo test (,); // you can use
Echo test (1, 5); // Error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.