0

I have a question how to call a variable.

In Js, if I write $A = $this->A inside of B(), I could reach $this->A through $A inside of C(); But in PHP seem like this way will not work.

Please advice a way that can read $this-A inside of C() without give function variable.

class X {

    function B(){
        function C(){
           $this->A;  // Can not call A here
           $A; // No work either

        }
        $A = $this->A; // 

        $this->A; // Can call A here
    }

    private $A;
}

This one here is Global $A Get variables from the outside, inside a function in PHP

However,if global, the global here means the entire script not within the class.

Thank you very much for your advice.

Community
  • 1
  • 1
Micah
  • 4,254
  • 8
  • 30
  • 38

2 Answers2

1

You cannot define functions within functions, avoid it, it is bad! The function will not be restricted to the scope. Functions are always global.

Although it will work on the first run, the execution of such code will only define the function then (it will be unavailable for calls before), and on the next call will crash with "redefined function error".

Knowing this, what your code does is define a global function "C" that is actually outside the class, and thus has no access to private properties.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • i am using if(!function_exists('')) for the function C() actually. will that work? – Micah Jan 23 '13 at 08:19
  • Doesn't matter. Functions are always global. It doesn't make sense to execute the code you have. What are you trying to achieve? Put the C function into the class just like the B function, probably making it a private function to disallow access from outside the class. – Sven Jan 23 '13 at 08:21
  • I see, I dont very understand that what you mean global here...I thought that all the function stated inside of a class within the class scope... – Micah Jan 23 '13 at 08:22
  • because I am using same function name for different class so far...it is not global scope for entire.... – Micah Jan 23 '13 at 08:23
  • Please read http://php.net/manual/en/language.variables.scope.php http://php.net/manual/en/language.oop5.visibility.php and http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php – Sven Jan 23 '13 at 08:44
1

As Sven said function within a function is bad however ignoring that you can also do this to expose non functional scope vars to within the function

class X {

    function B(){
        $A = $this->A; //
        function C(){
           global $A; // No work either
        }
        $this->A; // Can call A here
    }

    private $A;
}

EDIT from the comment:

class X { 

    public $inputvar; 
    public $outputvar; 

    public function B($changeinput) { 
        $this->inputvar = $changeinput; 
    } 

    public function C($secondinput) { 
        $this->outputvar = $this->inputvar." == ".$secondinput; 
    } 

    public function return_var() { 
        return $this->outputvar; 
    }
}

$testclass = new X(); 

$testclass->B("test input"); 
$testclass->C("second input"); 
$testclass->inputvar = "BLAAH"; 

echo $testclass->return_var();
Sven
  • 69,403
  • 10
  • 107
  • 109
Dave
  • 3,280
  • 2
  • 22
  • 40
  • Thank you very much, may I ask if the global $A inside of C(), this global will only mean inside of B()? of the entire page?? (assume if there is a $A outside of class – Micah Jan 23 '13 at 08:27
  • adding global $A inside of C will allow it to access any variable called $A that is specified as a global variable. ie a varaiable outside of C that isn't a private var. So it should be able to read $A FROM B() and from the primary class. eg `class X { public $test; function C() { global $test; } }` – Dave Jan 23 '13 at 08:36
  • @Dave: What are you trying to say? Your code tells me: "It does not work". Your introduction suggests otherwise. And by the way: Your code does not access $A that is inside B(), so it doesn't work. – Sven Jan 23 '13 at 08:41
  • inputvar = $changeinput; } function C($secondinput){ $this->outputvar = $this->inputvar." == ".$secondinput; } function return_var() { return $this->outputvar; } } $testclass = new X(); $testclass->B("test input"); $testclass->C("second input"); $testclass->inputvar = "BLAAH"; echo $testclass->return_var(); ?> – Dave Jan 23 '13 at 09:09
  • I still do not understand what you are trying to say. – Sven Jan 23 '13 at 16:33