PHP Object programming problem, call to a member function Hello () on a non-object
$instest = new test ();
$insobject = new Object ();
$insobject->objectvalue = "final";
$instest->test ();
Class test{
var $testValue = "Testvalueins";
function Test () {
Print_r ($insobject);
$insobject->hello ();
}
}
Class object{
var $objectValue = "original";
function Hello () {
Echo $objectValue;
}
}
?>
The error is as follows
notice:undefined Variable:insobject in C:\wamp\www\zhebo\test.php on line 11
Call Stack
notice:undefined Variable:insobject in C:\wamp\www\zhebo\test.php on line 12
Fatal Error:call to a member function Hello () on a non-object in C:\wamp\www\zhebo\test.php on line 12
What's the problem, how can you get to the method that references the other instance in the instance, or what better way to solve it?
I am in a hurry, I hope you can help. Thank you very much. Very urgent. The first time to use the idea of the object programming is not quite understand AH.
Share to:
------Solution--------------------
External variables cannot be accessed internally without passing or global declarations (objects are also used as vectors of variables)
This is the basic rule of PHP syntax, insurmountable
$insobject = new Object ();
$insobject->objectvalue = "final";
$instest = new test ($insobject);
$instest->test (); This is a constructor, which is generally not called
Class test{
var $testValue = "Testvalueins";
function test ($insobject) {
Print_r ($insobject);
$insobject->hello ();
}
}
Class object{
var $objectValue = "original";
function Hello () {
Echo $this->objectvalue; To access properties like this
}
}
Object Object ([ObjectValue] = final) Final
------Solution--------------------
The call constructor is the same as new
is to return an instance of a class