Connecting Tech Pros Worldwide Forums | Help | Site Map

[PHP] using object of a class in different files

realin's Avatar
Familiar Sight
 
Join Date: Feb 2007
Posts: 252
#1: Aug 10 '07
hi guys,

I have a php file say "class.php" in which a database class is defined.
I have got another file namely "config.php" which initializes its object as
Expand|Select|Wrap|Line Numbers
  1. require_once("class.php");
  2. $xReal=new class_name;
  3. global $xReal;
NOw i have made this $xReal object as global to access it in different files. So to access what i do is require("config.php");

but it wont access the functions inside class, saying Call to a member function rQuery() on a non-object

Please help me
thanks
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#2: Aug 10 '07

re: [PHP] using object of a class in different files


Hi,

I had a similar issue myself recently. I got around this without globals. The solution was to create an instance of the database object, then when creating an instance of another class pass the database object into it and set it as a property of the second class.

If you are simply wanting to use it on normal php pages, then I ahve also done this. All my site pages include pageheader.php. This file instantiates my database class. As this is included on all the pages the database object is available on all the pages.

I hope that makes sense.

Cheers
nathj
realin's Avatar
Familiar Sight
 
Join Date: Feb 2007
Posts: 252
#3: Aug 10 '07

re: [PHP] using object of a class in different files


i din get waht u want to say .. my bad, but can u explain it with example, plese

thanks :)
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#4: Aug 10 '07

re: [PHP] using object of a class in different files


Quote:

Originally Posted by realin

i din get waht u want to say .. my bad, but can u explain it with example, plese

thanks :)

Hi,

No need for an apology - my explanation was at best fuzzy. Here's an example of a class within a class:

Main php page (not class definition)
[php]
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/dataobjects.php');require_once($_SERVER['DOCUMENT_ROOT'].'/lib/userobject.php');
$loDB = new dataObject("DB", "user", "password", "host");
$loUser = new userObject($loDB);
?>
[/php]

So in this code I have created an instance of the dataObject and called it $loDB. I then create an instance of the userObject - $loUser. The userObject needs to have access to the functions on the dataObject. So I pass $loDB as a parameter to the userObject.

Here is the userObject (simplified)
[php]
<?php
class userObject
{
var $loDataObject ;
var $lnUserID ;
var $lnProfileID ;
var $lnOwnerType ;
var $lnLoginCount ;

public function userObject($poDB)
{
$this->loDataObject = $poDB ;
}
?>
[/php]

This then sets the property loDatObject on userObject to be the $loDB as definied in the inital php page. So within the functions on userObject you can say $this->$loDataObject->datafunction(). This then calls the functions on the dataobject from within the userObject.

I hope that explanation makes things a bit clearer. If not give me ashout and I'll try again.

Cheers
nathj
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#5: Aug 13 '07

re: [PHP] using object of a class in different files


Heya, realin.

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

The global keyword doesn't work that way.

'global' is actually an alias for:
Expand|Select|Wrap|Line Numbers
  1. $var =& $GLOBALS['var'];
  2.  
  3. // Equivalent to:
  4. global $var;
  5.  
Instead, you want to do this:
Expand|Select|Wrap|Line Numbers
  1. $GLOBALS['xReal'] = new class_name();
  2.  
realin's Avatar
Familiar Sight
 
Join Date: Feb 2007
Posts: 252
#6: Aug 13 '07

re: [PHP] using object of a class in different files


@nathj

hey mate thanks a lot for your help, i have just one dataclass, so i can make new object everytime on each page.. its not an issue.. the basic thing is i never wanted to do that.. instead use a global object that can call teh methods defined in my class.. anyways never mind.. but please come up with something that can get into my noob brain

thanks thanks much of thanks mann.. :)




@pbmods,

heya thanks for the reply.. but that wont work even :(
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#7: Aug 13 '07

re: [PHP] using object of a class in different files


Quote:

Originally Posted by realin

@nathj

hey mate thanks a lot for your help, i have just one dataclass, so i can make new object everytime on each page.. its not an issue.. the basic thing is i never wanted to do that.. instead use a global object that can call teh methods defined in my class.. anyways never mind.. but please come up with something that can get into my noob brain

thanks thanks much of thanks mann.. :)

My preference is always to sterr away from globals where possible. I think that instantiating the class on each page is a fine way to do it. Alternatively, and I have never tried this, it's just a theory and comes with no guarantees. Could you instantiate the class and load it into the session:
[php]
<?php
session_start();
$loDataClass = new dataClass() ;
$_SESSION['dataObject'] = $loDataClass ;
?>
[/php]
You may then have to do the inverse to use the functions and pproperties of the class, ie take it from $_SESSION to a local variable?

I must confess I think this is a little dubious as you have the question of where to instantiate the data class? If you only run the instantiation on one page what happens if a visitor comes in via a different route - they don't come direct to the front page? If you instantiate the object on each page then it's fine, and there's no need for globals and no need for my dodgy idea - which may just be a load of old tosh anyway.

I think the answer is to simply instantiate the object on each page of the site.

Cheers
nathj
realin's Avatar
Familiar Sight
 
Join Date: Feb 2007
Posts: 252
#8: Aug 13 '07

re: [PHP] using object of a class in different files


@nathj

yeah mann even i think its a better idea to make new objects everytime i need them..

Can i have the same name of the objects on different pages ? i suppose it remains valid on jus the current page and contents .
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#9: Aug 13 '07

re: [PHP] using object of a class in different files


Heya, Realin.

Quote:

Originally Posted by realin

Can i have the same name of the objects on different pages ? i suppose it remains valid on jus the current page and contents .

Have a look at this article.
realin's Avatar
Familiar Sight
 
Join Date: Feb 2007
Posts: 252
#10: Aug 13 '07

re: [PHP] using object of a class in different files


woooooooooooooooo....

much of thanks nathj, i have adopted that pasing of object to constructor method..

damn taht rox :) and so do you :)

thanks a lot


@pbmods

thanks a lot for the valuable info, but i read on many articles not to use global objects :)

REQUEST :- can you add email notification, while posting or replying.. i have edit each post and get it done.. thanks a lot :)
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#11: Aug 13 '07

re: [PHP] using object of a class in different files


Heya, Realin.

Quote:

Originally Posted by realin

REQUEST :- can you add email notification, while posting or replying.. i have edit each post and get it done.. thanks a lot :)

It's on our todo list.
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#12: Aug 13 '07

re: [PHP] using object of a class in different files


Quote:

Originally Posted by realin

woooooooooooooooo....

much of thanks nathj, i have adopted that pasing of object to constructor method..

damn taht rox :) and so do you :)

thanks a lot

Thanks for the compliment - I'm just glad to have helped.

Cheers and all the best with the rest of the project.
nathj
realin's Avatar
Familiar Sight
 
Join Date: Feb 2007
Posts: 252
#13: Aug 13 '07

re: [PHP] using object of a class in different files


Quote:

Originally Posted by nathj

Cheers and all the best with the rest of the project.
nathj


thanks once again for wishing me luck :)
Reply