PHP object programming problem, Calltoamemberfunctionhello () onanon-object

Source: Internet
Author: User
PHP object programming problem, Calltoamemberfunctionhello () onanon-object
 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 following error is reported:

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

Is there any problem, how can we reference methods in other instances in the instance, or what is a better solution?
I am in a hurry. I hope you can help me. Thank you very much. Very urgent. The first time I used object programming, I still don't know much about it.


Reply to discussion (solution)

$instest = new test();$insobject = new object();//$insobject->objectValue = "final";object::$objectValue= "final";$instest->test();    class test{        var $testValue = "testValueins";        function test(){           object::hello();        }    }    class object{        public static $objectValue = "original";        Public static function hello(){            echo self::$objectValue.'
'; } }

? ? ?? You ?? No ??, Regret it,

? ? ?? You ?? No ??, Regret it,

No, the Post has not been able to solve the problem. of course, I have to wait and paste it again :)

? ? ?? You ?? No ??, Regret it,

Does it seem that static data can only be used?

External variables cannot be accessed internally without being passed or declared globally (the object uses variables as the carrier)
This is the basic rule of php syntax and is insurmountable.

$ Insobject = new object (); $ insobject-> objectValue = "final"; $ instest = new test ($ insobject); // $ instest-> test (); this is a constructor. generally, class test {var $ testValue = "testValueins" is not called in this way; function test ($ insobject) {print_r ($ insobject ); $ insobject-> hello () ;}} class object {var $ objectValue = "original"; function hello () {echo $ this-> objectValue; // access attributes like this }}
Object Object ([objectValue] => final) final

External variables cannot be accessed internally without being passed or declared globally (the object uses variables as the carrier)
This is the basic rule of php syntax and is insurmountable.

$ Insobject = new object (); $ insobject-> objectValue = "final"; $ instest = new test ($ insobject); // $ instest-> test (); this is a constructor. generally, class test {var $ testValue = "testValueins" is not called in this way; function test ($ insobject) {print_r ($ insobject ); $ insobject-> hello () ;}} class object {var $ objectValue = "original"; function hello () {echo $ this-> objectValue; // access attributes like this }}
Object Object ([objectValue] => final) final

So how should I call the test function in test externally? If not


External variables cannot be accessed internally without being passed or declared globally (the object uses variables as the carrier)
This is the basic rule of php syntax and is insurmountable.

$ Insobject = new object (); $ insobject-> objectValue = "final"; $ instest = new test ($ insobject); // $ instest-> test (); this is a constructor. generally, class test {var $ testValue = "testValueins" is not called in this way; function test ($ insobject) {print_r ($ insobject ); $ insobject-> hello () ;}} class object {var $ objectValue = "original"; function hello () {echo $ this-> objectValue; // access attributes like this }}
Object Object ([objectValue] => final) final

So how should I call the test function in test externally? If not

Besides, isn't the construct?

Calling constructor is the same as calling new.
Returns an instance of a class.



External variables cannot be accessed internally without being passed or declared globally (the object uses variables as the carrier)
This is the basic rule of php syntax and is insurmountable.

$ Insobject = new object (); $ insobject-> objectValue = "final"; $ instest = new test ($ insobject); // $ instest-> test (); this is a constructor. generally, class test {var $ testValue = "testValueins" is not called in this way; function test ($ insobject) {print_r ($ insobject ); $ insobject-> hello () ;}} class object {var $ objectValue = "original"; function hello () {echo $ this-> objectValue; // access attributes like this }}
Object Object ([objectValue] => final) final

So how should I call the test function in test externally? If not

Besides, isn't the construct?

I remember, the default constructor is the same name function in php4.


? ? ?? You ?? No ??, Regret it,

Does it seem that static data can only be used? ?? Faster, no? Look at you? It seems like? Helpless ,? What?

The same is true for php5 and for C ++.




External variables cannot be accessed internally without being passed or declared globally (the object uses variables as the carrier)
This is the basic rule of php syntax and is insurmountable.

$ Insobject = new object (); $ insobject-> objectValue = "final"; $ instest = new test ($ insobject); // $ instest-> test (); this is a constructor. generally, class test {var $ testValue = "testValueins" is not called in this way; function test ($ insobject) {print_r ($ insobject ); $ insobject-> hello () ;}} class object {var $ objectValue = "original"; function hello () {echo $ this-> objectValue; // access attributes like this }}
Object Object ([objectValue] => final) final

So how should I call the test function in test externally? If not

Besides, isn't the construct?

I remember, the default constructor is the same name function in php4.

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.