-1
class ABC{
    public function __construct(){}
    // There have a parameter
    public function check($data){
    // There have a variable
        $available = null;
        if(true){
          $available = true;
        }else{
          $available = false;
        }
    }
}


$obj= new ABC();

// I want to access this $available

$obj->available;

How can I access $available value from the class method? Is there a dynamic parameter in this method (this is the problem)?

1
  • you cant access a variable from a class method. you can set a public property $available and then access it. Commented Dec 3, 2016 at 7:48

1 Answer 1

1

Currently $available is only visible in the scope of your check function. You need to create a local variable for $available and set its visibility to public then you can change that variable in the ABC class and access it from outside of the class.

<?php 
    class ABC {
        public $available = true;

        public function __construct(){}
        public function check($data){
            // There have a variable

            if($data){
                $available = true;
            }else{
              $available = false;
            }
        }

        // create an optional getter for the variable 
        public function isAvailable() {
            return $available;
        }
    }

$obj= new ABC();

// I want to access this $available
echo $obj->available

// or access it through the getter
echo $obj->isAvailable()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.