1

Hey all actually I too facing the problem but I couldn't understand any of the above methods. Please help me to understand those stuffs and help t fix my problem.

I have two methods method1 and method2, where I receive some value in method 1 which needs to used in method 2. I created a variable on class level but I couldn't access the variable below is the code snippet.

class testController extends controller
{
    public $isChecked = false;
    public $isSelectedValue = 0;

    public function ValidateValue(Request $req)
    {
        $isChecked = $req->checked;
        $isSelectedValue = $req->value;
    }

    public function UsethoseValues()
    {
        if ($isChecked) { // I can't use the variable here it throws run time error. I need help on this please help.
        }
    }
}
6
  • 3
    you need to write $this->isChecked not $isChecked Commented Dec 27, 2019 at 13:51
  • 1
    Would probably help to read the manual on class properties. Commented Dec 27, 2019 at 14:06
  • 1
    @Joseph that solved my issue thanks Commented Dec 27, 2019 at 14:48
  • @GopiP accept the answer if it helped you, and thanks Commented Dec 27, 2019 at 14:51
  • @Jeto I recommend call class in CameCase it a good practice. There are PSR describes those practices. It's fine when you work alone to not use any recommendation but when you going to work in a team member going to hate you. Commented Dec 27, 2019 at 15:21

1 Answer 1

1

because you are in class and you declare a property not a simple variable so when you try to access it from the method in your class you need to add $this

keyword that refer to your class

$this->isChecked

so your code will be like this after editing

class testController extends controller { 
        public $isChecked = false;
        public $isSelectedValue = 0;
public function ValidateValue(Request $req) {
        $this->isChecked = $req->checked;
        $this->isSelectedValue = $req->value;
     }
public function UsethoseValues() {
        if($this->isChecked) { // I can't use the variable here it throws run time error. I need help on this please help.
           }
     }
}

feel free to check the docs for more info

Sign up to request clarification or add additional context in comments.

3 Comments

thanks. I need another help. the value for $isChecked is reassigned in Validatevalue() method. which needs to be used in the usethoseValues() method. But, the value on $isChecked is not getting modified. Can you please help me on this?
@GopiP if you answer a new question with your problem it would be more clear

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.