Page 1 of 1

Object from a included page

Posted: Tue Aug 26, 2008 4:30 am
by pcoder
hi to all, after a long time.
One thing really making me tense.
Can't we use object from the included page.
I have added common class and created object of used class in one page. And included this page in another page and try to use the object
from the included page.
Sometimes it's ok and works but sometimes throws an error like:
>>>Call to a member function {function name} on a non-object
And cannot used that object.
So what will be the solution for this?

Thanks in advance

Re: Object from a included page

Posted: Tue Aug 26, 2008 5:01 am
by Ziq
Can u show source code?

Re: Object from a included page

Posted: Tue Aug 26, 2008 5:06 am
by pcoder
I have not declared the object as global
fileName:objectCreated.php

Code: Select all

 
require('class/Decease.php');
$objDecease = new Decease();
print 'created'.$objDecease;
 
fileName:objectCalled.php

Code: Select all

 
 
require "objectCreated.php";
print 'called'.$objDecease;
 

Re: Object from a included page

Posted: Tue Aug 26, 2008 5:08 am
by Ziq
Two variants are possible:
1. Variable this object rewrite
2. Variable this object not set or empty

Re: Object from a included page

Posted: Tue Aug 26, 2008 5:10 am
by pcoder
And the object from the objectCalled.php cannot call the function of Decease Class.
I just created the example of what i said
Thanks

Re: Object from a included page

Posted: Tue Aug 26, 2008 5:22 am
by Ziq
Debug objectCalled.php. Insert this code before use {function}:

Code: Select all

 
var_dump($objDecease);
 
This error arises in this situation:

Code: Select all

 
$a = "I'm not object";
$a->func();
// Result: Fatal error: Call to a member function func() on a non-object
 
Consequently, variable $objDecease not object. Your task find place where $objDecease lose value (object type).

Re: Object from a included page

Posted: Tue Aug 26, 2008 5:35 am
by pcoder
Thanks for the quick reply ziq,
Yeah i am trying to find out the place where this object lose it's object type.
But if i declared that object as global, it works perfectly. I think it is not necessary to declare as global to access the object.

Thanks

Re: Object from a included page

Posted: Tue Aug 26, 2008 6:02 am
by Ziq
Are you use $objDecease inside function?

Re: Object from a included page

Posted: Tue Aug 26, 2008 6:10 am
by pcoder
Thanks ziq for the prompt responce
Ok wait , i will tell you my structure in more details.
I have a file, let's say a.php, which includes the page named Init.php which is inside the local hierarchy.
And this Init file also include the another file Init.php which is placed outside and common for all.
I have imported class file and created object of these class in this Inti.php file.
And i have not used object inside the function and try to use the object property from the file a.php

Re: Object from a included page

Posted: Tue Aug 26, 2008 6:40 am
by pcoder
Sorry ziq, i got the error, you are right.
Init.php file is imported through the function, :)
Is there any way to access this object without declaring it as global

Thanks

Re: Object from a included page

Posted: Tue Aug 26, 2008 6:44 am
by Ziq
I have already started to think that it is magic :D

Re: Object from a included page

Posted: Tue Aug 26, 2008 12:02 pm
by Ziq
Is there any way to access this object without declaring it as global
No

Re: Object from a included page

Posted: Tue Aug 26, 2008 2:41 pm
by Christopher
pcoder wrote:Is there any way to access this object without declaring it as global
I is best to pass the object into the function that uses it. Or the the constructor of an class that uses it.