Object from a included page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Object from a included page

Post 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
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Object from a included page

Post by Ziq »

Can u show source code?
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Object from a included page

Post 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;
 
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Object from a included page

Post by Ziq »

Two variants are possible:
1. Variable this object rewrite
2. Variable this object not set or empty
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Object from a included page

Post 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
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Object from a included page

Post 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).
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Object from a included page

Post 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
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Object from a included page

Post by Ziq »

Are you use $objDecease inside function?
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Object from a included page

Post 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
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Object from a included page

Post 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
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Object from a included page

Post by Ziq »

I have already started to think that it is magic :D
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Object from a included page

Post by Ziq »

Is there any way to access this object without declaring it as global
No
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Object from a included page

Post 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.
(#10850)
Post Reply