"Emergency help" Fatal Error:call to a member function * * * on a Non-object

Source: Internet
Author: User
Tags variable scope
This post was last edited by keric2008 on 2013-08-26 18:33:02

PHP class

The source code is very long, abstract, the approximate process is this.

{
$instest = new test ();
$insobject = new Object ();
$instest->test ();
}

Class test{
function Test () {
$insobject->hello ();
}
}

Class object{
function Hello () {
echo "Hello";
}
}
This will cause title error: Fatal Error:call to a member function Hello () on a non-object in/home/latelx64/workspace/zhebo/init.php on Line 158

Is it possible to invoke the public function of other instances in the instance?

If not, how should it be solved in a similar way???
I am a newly-learned PHP, I hope you master to give guidance!!!


Reply to discussion (solution)

If you have to do this, then the function needs to pass the argument and pass an object in.

{$instest = new test (), $insobject = new Object (); $instest->test ($insobject);} Class Test{function Test ($insobject) {$insobject->hello ();}} Class Object{function Hello () {echo "Hello";}}


In general, from the perspective of design, the use of inheritance will be better.

Can I use inheritance for an example, thank you very much

The source code is very long, abstract, the approximate process is this.

{
$instest = new test ();
$insobject = new Object ();
$instest->test ();
}

Class test{
function Test () {
$insobject->hello ();
}
}

Class object{
function Hello () {
echo "Hello";
}
}
This will cause title error: Fatal Error:call to a member function Hello () on a non-object in/home/latelx64/workspace/zhebo/init.php on Line 158

Is it possible to invoke the public function of other instances in the instance?

If not, how should it be solved in a similar way???
I am a newly-learned PHP, I hope you master to give guidance!!!

In the case of inheritance, the data in the instance of the parent class declaration will not exist in the instance using the subclass declaration.

This is not object-oriented!
It's just a matter of variable scope!
Class test{
function Test () {
$insobject->hello ();
}
}
The $insobject is a local variable and is not assigned a value.
Of course, there's no Hello method.

Do you want to declare global variables in each function?

This is not object-oriented!
It's just a matter of variable scope!
Class test{
function Test () {
$insobject->hello ();
}
}
The $insobject is a local variable and is not assigned a value.
Of course, there's no Hello method.

Class test{
function Test () {
Global $insobject;
$insobject->hello ();
}
}
So every function must be declared not very complicated!! Is there nothing to be tactful about?


This is not object-oriented!
It's just a matter of variable scope!
Class test{
function Test () {
$insobject->hello ();
}
}
The $insobject is a local variable and is not assigned a value.
Of course, there's no Hello method.

$_env['? Volume name ']

? Things can be super-global use, like?:

$_globals['?];$_get[', volume name '];$_post['? Volume name '];$_request['?

If you have to do this, then the function needs to pass the argument and pass an object in.

{$instest = new test (), $insobject = new Object (); $instest->test ($insobject);} Class Test{function Test ($insobject) {$insobject->hello ();}} Class Object{function Hello () {echo "Hello";}}


In general, from the perspective of design, the use of inheritance will be better.


You can't do it with this method. warning:missing Argument 1 for Test::test (), called in C:\wamp\www\zhebo\index.php on line 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";    }}
  • Related Article

    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.