Learn PHP class, encounter a basic problem, ask good people answer.

Source: Internet
Author: User
Code such as, blue to perform the display 1, red Display 6, the need is to display 6;

How can BBB () use the $c in AAA ()? Only by the way in the red box? (Feel Good Trouble)
If you don't use the same method in the red box, but there are many variables in AAA () that need to be used in BBB (), what do I do with $c?


Reply to discussion (solution)

You're a function of the argument.

If the function bbb () {} does not specify an incoming parameter when it is declared, then even if you call the time, it is useless to pass in more, always return $c that is 1. You have to specify the function parameters to be able to line.

This sentence in your BBB method.
$c = $c + 1;
There will be a notice:undefined variable:c warning ($c value is not defined)
Although you have blocked the E_notice level of error messages, it does not indicate that there are no problems. Despite the situation here,

Class Ceshi {public  function aaa ($a) {    $this->c = $a;    $r = $this->bbb ();    echo $r;  }  Public Function bbb () {    $c = $this->c + 1;    return $c;  }} $XYZ = new Ceshi; $xyz->aaa (5);

This will output 6.

Use the class's properties class Ceshi {private $c;  Public function aaa ($a) {    $this->c = $a;    echo $this->bbb ();  }  Public Function bbb () {    return $this->c + 1;  }} $XYZ = new Ceshi; $xyz->aaa (5);

Use?? The amount of sex is available.

Private $c; Any function in any of these can be used.

Class ceshi{private $c;p ublic function aaa ($a) {$this->c = $a; $k = $this->bbb (); Echo $k;} Public Function bbb () {$this->c = $this->c + 1;return $this->c;}} $XYZ = new Ceshi; $xyz->aaa (5);

Variable scope problem, 4 floor method can be used ha

Thank you upstairs!

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