Simple PHP Classes Question
So, I have a:
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' ??
source to share
First, when you are just writing $variable_one;
internally A()
, this is not the case for your class member variables! It would be a completely different, newly created local variable called $variable_one
, unrelated to the class variable.
Instead, you want:
function A() {
$this->variable_one;
}
Second, yours is $variable_3
also a local variable and wo n't be available in any other function.
Third, your assignments at the bottom are correct in form, but not in syntax: there is an additional one $
. Do you want to:
$myObj->variable_one = 'some value';
source to share
No, $variable_3
was created (and will be destroyed) in the area function_A
. This is due to the capabilities of the function.
http://us3.php.net/manual/en/language.variables.scope.php
If you want $ variable_3 to be persisted by your object after execution leaves the scope of function_A, 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
}
}
source to share
The correct code would be as follows: (see the answer in the 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 are not). If you don't use the $ this-> prefix, they will be local variables within the function that you define them.
Hope this helps!
source to share
If variable_one
they variable_two
are public
, you can assign them as you specified (just remove the "$" ... so $ classObject-> variable_one). Usually, you want to encapsulate your variables by making them 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!
source to share