PHP Object Programming questions, call to a member function Hello () on a non-object

Source: Internet
Author: User
Tags access properties
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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.