tags:

views:

93

answers:

5

So I have:

class foo {
  public $variable_one;
  public $variable_two;

  function_A(){
    // Do something
    $variable_one;
    $variable_two;

    // If I define variable_3 here!
    $variable_3
    // Would I be able to access it in function_B?
  }

  function_B(){
    // Do something
    $variable_4 = $variable_3
  }
}


$myObj = new foo();
// Now what do I write in order to assign "variable_one" and "two" some value?
$myObj->$variable_one = 'some_value' ??
$myObj->$variable_two = 'some_value' ??
+2  A: 

No, $variable_3 was created (and will be destroyed) in the scope of function_A. This is due to function scope.

http://us3.php.net/manual/en/language.variables.scope.php

If you would like $variable_3 to be retained by your object once execution leaves function_A's scope, you need to assign it as a class property, similar to $variable_1 and $variable2.

class YourClass
{

    public $variable_1;
    public $variable_2;
    public $variable_3;

    function_A()
    {
        $this->variable_3 = "some value"; // assign to the object property
        $variable_4 = "another value"; // will be local to this method only
    }

    function_B()
    {
        echo $this->variable_3; // Would output "some value"
        echo $variable_4; // var does not exist within the scope of function_B
    }

}
localshred
+7  A: 

First, when you write simply $variable_one; inside A() it does not refer to the member variables of your class! That would be a completely different, newly created local variable called $variable_one bearing no relation to the class variable.

Instead, you want:

function A() {
    $this->variable_one;
}

Second, your $variable_3 is also a local variable, and will not be accessible in any other function.

Third, your assignments at the bottom are correct in form, but not in syntax: there's an extra $ in there. You want:

$myObj->variable_one = 'some value';
VoteyDisciple
+1  A: 
$myObj->variable_one = aValue;
$myObj->variable_two = anotherValue;
tomzx
A: 

If variable_one and variable_two are public, you can assign them as you specified (just remove the "$"...so $classObject->variable_one). Typically you want to encapsulate your variables by making them either protected or private:

class MyClass
{
    protected $_variable_one;

    public function getVariableOne()
    {
        return $this->_variable_one;
    }

    public function setVariableOne($value)
    {
        $this->_variable_one = $value;
    }
}

$c = new MyClass();
$c->setVariableOne("hello!");

echo $c->getVariableOne(); // hello!
Typeoneerror
+1  A: 

The correct code would be the following (see answer within comments)

class foo {
  public $variable_one;
  public $variable_two;
  private $variable_three; // private because it is only used within the class

  function _A(){
    // using $this-> because you want to use the value you assigned  at the
    // bottom of the script. If you do not use $this-> in front of the variable, 
    // it will be a local variable, which means it will be only available inside
    // the current function which in this case is _A
    $this->variable_one; 
    $this->variable_two;

    // You need to use $this-> here as well because the variable_3 is used in
    // function _B
    $this->variable_3;
  }

  function _B(){
    // Do something
    $variable_4 = $this->variable_3
  }
}


$myObj = new foo();
$myObj->variable_one = 'some_value1'; // Notice no $ in front of the var name
$myObj->variable_two = 'some_value2'; // Notice no $ in front of the var name

Class variables (properties) must be accessed using the $this-> prefix, unless they are static (in your example they aren't). If you do not use the prefix $this-> they will be local variables within the function you define them.

I hope this helps!

philhq