Wrong? I do not know. What are you trying to do?
Are you trying to assign a value to variable in the method-level variable scope? A value that will not be there the next time the method is run? Then your code will do admirably.
Are you trying to assign a value to the class-level variable-scoped variable you created with the line "public $hope;"? A value that will exists for the life of the object instantiated form the class definition? If so, you have to use "$this->hope".
Perhaps another example is in order.
Code:
<?php
class foo
{
protected $bar; //create a class-scoped variable
function function1 ()
{
$bar = 3; //assign a value to a method-level variable
$this->bar = 4; //assign a value to a class-level variable
print $bar . '<br>';
print $this->bar . '<br>';
}
function function2 ()
{
print $bar; //output value from a method-level variable
print $this->bar; //output value from a class-level variable
}
}
$a = new foo();
$a->function1();
$a->function2();
?>
NOTE: I am doing a very unwise thing in my code, specifically using the same variable name at the method-level scope and the class-level scope. I am doing this strictly for the purposes of pedagogy. Under no circumstances should one do this in production code.
The output of this script on my system is:
3
4
Notice: Undefined variable: bar in /home/sites/test/html/test_class.php on line 18
4
It outputs the value of the method-scope variable then the class-scope variable.
Then it outputs an error for the non-existence of the method-scope variable that only existed for the time the function1() method ran. After that, the variable no longer existed because its scope no longer existed.
Then it outputs the existing class-scoped variable.
Want the best answers?
Ask the best questions! TANSTAAFL!