Custom functions How to understand the parameters in this way

Source: Internet
Author: User
The following is a piece of code in a class
static function Start (PDO $pdo) {self::init ($PDO); Session_set_save_handler (Array (__class__, "open"), Array (__class__ , "close"), Array (__class__, "read"), Array (__class__, "write"), Array (__class__, "destroy"), Array (__class__, "GC")); Session_Start ();}

The PDO in this function is a parameter, which is confusing because I understand that the function is to pass a parameter and return the result of the returned value, and how does this string pass?


Reply to discussion (solution)

function Start (PDO $pdo) {
Formal parameter $pdo
Type declaration PDO
That is, the start method must pass in an object based on the PDO class
If you do not add a type declaration (traditional practice is not added) you may need to check whether the incoming parameters are appropriate
But when added, the PHP parser will help you check that the parameter type is correct.

function Start (PDO $pdo) {
Formal parameter $pdo
Type declaration PDO
That is, the start method must pass in an object based on the PDO class
If you do not add a type declaration (traditional practice is not added) you may need to check whether the incoming parameters are appropriate
But when added, the PHP parser will help you check that the parameter type is correct.
Suddenly understand, if it is a formal parameter to be separated by commas, and this code does not, feel the moderator said the rational. But I'm still a little dizzy, this is not a lot of PHP tutorial, I check the manual.

There are three types of formal parameters: value passing, reference passing, and default parameter passing three mechanisms.
(1) Value transfer
function sum ($a, $b)
{
Echo $a + $b;
}
SUM (10,20); Start calling the function
?>
(2) Reference passing

$myNum = 100;
function Valuechange ($number)
{
$number = $number +1;
Echo $number. "
";
}
Valuechange (& $myNum);
Echo $myNum;
?>
Results: 101,101
(3) Default parameter passing
function Hobby ($style = "Motion")
{
Return "I like $style
";
}
echo Hobby ();
Echo Hobby ("singing");
?>
Result: I like sports
I like singing.

  • Related Article

    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.