The same class in PHP reads the variable of another function-php Tutorial

Source: Internet
Author: User
In PHP, how does one call A variable value in function B in function A to read another function variable in the same class?


Reply to discussion (solution)

Impossible!
Read the scope description of variables carefully in the manual.

It can be set as A global variable when the variable in B is defined, or you can use other methods to return the value to.

To implement the same class, you can only change the variable to the attribute of this class.

To implement the same class, you can only change the variable to the attribute of this class.



For example, if I want to use the value in B in A, what should I do?

No, the scope is limited.

If you want to read all of them, you can set them as global variables of the class.

Class declares an attribute $ c assigned $ this-> c = A in method 1, and $ this-> c can be directly used in method 2. You must call method 1 before assigning values.

No, the scope is limited.

If you want to read all of them, you can set them as global variables of the class.



Can I set it to public $ a at the beginning of class?

Class declares an attribute $ c assigned $ this-> c = A in method 1, and $ this-> c can be directly used in method 2. You must call method 1 before assigning values.



What does it mean? I declare it public $ a. Then, assign a value according to your description in method 1, and then method 2 cannot read it.

class T {  public $v;  function B() {    $this->v = 123;  }  function A() {    echo $this->v;  }}$p = new T;$p->B();$p->A(); //123

class T {  public $v;  function B() {    $this->v = 123;  }  function A() {    echo $this->v;  }}$p = new T;$p->B();$p->A(); //123




If I write
class T{public  $p;function a{$this->p=1;}function b{echo $this->p;}}


After the page comes in, I will first read function a and then read function B?


class T {  public $v;  function B() {    $this->v = 123;  }  function A() {    echo $this->v;  }}$p = new T;$p->B();$p->A(); //123




If I write
class T{public  $p;function a{$this->p=1;}function b{echo $this->p;}}


After the page comes in, I will first read function a and then read function B?



This is not the same as above. assign values first and then read

What do you think will happen?
How can we implement A and B first?In function A, call A variable value in function B to use

class T {  function B() {    return 123;  }  function A() {    echo $this->B();  }}$p = new T;$p->B();$p->A(); //123
But this is not the value in B.

Therefore, your basic starting point is problematic.



class T {  public $v;  function B() {    $this->v = 123;  }  function A() {    echo $this->v;  }}$p = new T;$p->B();$p->A(); //123




If I write
class T{public  $p;function a{$this->p=1;}function b{echo $this->p;}}


After the page comes in, I will first read function a and then read function B?



This is not the same as above. assign values first and then read



I just tried this and I don't know how to read a null value ..

class T {  function B() {    return 123;  }  function A() {    echo $this->B();  }}$p = new T;$p->B();$p->A(); //123
But this is not the value in B.

Therefore, your basic starting point is problematic.



class T{public  $p;function a{$this->p=1;}function b{echo $this->p;}}


I wrote this. In fact, the business logic I want to implement is that after the page comes in, I asynchronously read Method a, and then I need the value in method a in Method B, I don't know if I can get the value of Method a in Method B.

Is the execution of A and B not in the same http session?
This is wrong for you!
Other carriers are needed to implement

Is the execution of A and B not in the same http session?
This is wrong for you!
Other carriers are needed to implement




That is, when the page comes in, it is the method, and then the B method is used asynchronously. so there are two different calls. can't variables be used for each other?

Yes! Data cannot be shared between different sessions.
Therefore, cookies, Sessions, databases, files, and third-party software are required.

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.