[Urgent help] Fatalerror: Calltoamemberfunction *** onanon-object

Source: Internet
Author: User
[Urgent help] Fatalerror: Calltoamemberfunction *** onanon-object This post was last edited by keric2008 at 18:33:02

Php class

The source code is very long. Abstract: The process is like this.

{
$ Instest = new test ();
$ Insobject = new object ();
$ Instest-> test ();
}

Class test {
Function test (){
$ Insobject-> hello ();
}
}

Class object {
Function hello (){
Echo "hello ";
}
}
In this case, the following error occurs: Fatal error: Call to a member function hello () on a non-object in/home/latelx64/workspace/zhebo/init. php on line 158

Can't the public functions of other instances be called in the instance?

If not, how can we solve the problem in a similar way ???
I am new to PHP, and hope you can give guidance !!!


Reply to discussion (solution)

If you have to do this, the function needs to pass the parameter and upload an object.

{$instest = new test();$insobject = new object();$instest->test($insobject);}class test{function test($insobject){$insobject->hello();}}class object{function hello(){echo "hello";}}


Generally, from the design point of view, it is better to use the inheritance method.

Can I use inheritance as an example? thank you very much.

The source code is very long. Abstract: The process is like this.

{
$ Instest = new test ();
$ Insobject = new object ();
$ Instest-> test ();
}

Class test {
Function test (){
$ Insobject-> hello ();
}
}

Class object {
Function hello (){
Echo "hello ";
}
}
In this case, the following error occurs: Fatal error: Call to a member function hello () on a non-object in/home/latelx64/workspace/zhebo/init. php on line 158

Can't the public functions of other instances be called in the instance?

If not, how can we solve the problem in a similar way ???
I am new to PHP, and hope you can give guidance !!!

If inheritance is used, the data in the instance declared by the parent class does not exist in the instance declared by the subclass.

This has nothing to do with object-oriented!
It's just a variable scope problem!
Class test {
Function test (){
$ Insobject-> hello ();
}
}
$ Insobject is a local variable, and no value is assigned.
Of course there is no hello method.

Do I need to declare global variables in each function?

This has nothing to do with object-oriented!
It's just a variable scope problem!
Class test {
Function test (){
$ Insobject-> hello ();
}
}
$ Insobject is a local variable, and no value is assigned.
Of course there is no hello method.

Class test {
Function test (){
Global $ insobject;
$ Insobject-> hello ();
}
}
In this way, it is not very complicated to declare in every function !! Is there no euphemism?


This has nothing to do with object-oriented!
It's just a variable scope problem!
Class test {
Function test (){
$ Insobject-> hello ();
}
}
$ Insobject is a local variable, and no value is assigned.
Of course there is no hello method.

$ _ ENV ['? Volume name']

? It can be used globally ,? Like? Include:

$ _ GLOBALS ['? Volume name ']; $ _ GET ['? Volume name ']; $ _ POST ['? Volume name ']; $ _ REQUEST ['? Volume name'] and so on

If you have to do this, the function needs to pass the parameter and upload an object.

{$instest = new test();$insobject = new object();$instest->test($insobject);}class test{function test($insobject){$insobject->hello();}}class object{function hello(){echo "hello";}}


Generally, from the design point of view, it is better to use the inheritance method.


You cannot use this method. Warning: Missing argument 1 for test: test (), called in C: \ wamp \ www \ zhebo \ index. php on line 24 and defined in C: \ wamp \ www \ zhebo \ x \ init. php on line 170

class test{    function test(){        object::hello();    }}class object{    Public static function hello(){        echo "hello";    }}

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.