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.