A variable in PHP that reads another function from the same class

Source: Internet
Author: User
In the same class, how do you invoke a variable value from function B in function a?


Reply to discussion (solution)

No way!
Please read carefully the description of the scope of variables in the manual

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

In the same class, to implement a variable that can only be used as a property of this class.

In the same class, to implement a variable that can only be used as a property of this class.



So what if I use the value of B in a?

No, scope is limited.

If you want to read it, you can set it as a global variable for the class.

The $c class declares an attribute in Method 1 and $this->c=a; Method 2 uses $this->c directly. Be sure to call Method 1 before you can assign a value

No, scope is limited.

If you want to read it, you can set it as a global variable for the class.



I set it to public $a at the beginning of the class; Is that OK?

The $c class declares an attribute in Method 1 and $this->c=a; Method 2 uses $this->c directly. Be sure to call Method 1 before you can assign a value



What does it declare? I declare it as public $a; Then method 1 assigns the value you say, 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 this
Class T{public  $p; function a{$this->p=1;} function B{echo $this->p;}}


And then I'm going to read function A and then read function B after the page comes in?


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 this
Class T{public  $p; function a{$this->p=1;} function B{echo $this->p;}}


And then I'm going to read function A and then read function B after the page comes in?



This is not the same as the above, the first assignment, then read

What do you think is going to happen?
If first A and B, how to be reflected in function A, call function B in the value of a variable to use

Class T {  function B () {    return 123;  }  function A () {    echo $this->b ();  }} $p = new T; $p->b (); $p->a (); 123
But that's not the value in the call B.

So, your basic starting point is a problem.



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 this
Class T{public  $p; function a{$this->p=1;} function B{echo $this->p;}}


And then I'm going to read function A and then read function B after the page comes in?



This is not the same as the above, the first assignment, then read



Just tried this and then didn'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 that's not the value in the call B.

So, your basic starting point is a problem.



Class T{public  $p; function a{$this->p=1;} function B{echo $this->p;}}


I write this, in fact, I want to implement the business logic is that the page came in, asynchronous read a method, and then I in the B method also need a method in the value, I do not know I write this, can not achieve I in the B method to get the value of a method

Do a and B not be in the same HTTP session?
Then it's wrong of you to write like this!
Need to use other vectors to achieve

Do a and B not be in the same HTTP session?
Then it's wrong of you to write like this!
Need to use other vectors to achieve




Is that my page comes in is a method, and then asynchronously with the B method, so that is 2 different reply, the variables can not be used to each other, right?

Right! The data cannot be shared between different sessions.
So you need cookies, sessions, databases, files, and third-party support software.

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