473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

[PHP] using object of a class in different files

realin
254 100+
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
Aug 10 '07 #1
12 8973
nathj
938 Expert 512MB
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
Aug 10 '07 #2
realin
254 100+
i din get waht u want to say .. my bad, but can u explain it with example, plese

thanks :)
Aug 10 '07 #3
nathj
938 Expert 512MB
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
Aug 10 '07 #4
pbmods
5,821 Expert 4TB
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.  
Aug 13 '07 #5
realin
254 100+
@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 :(
Aug 13 '07 #6
nathj
938 Expert 512MB
@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
Aug 13 '07 #7
realin
254 100+
@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 .
Aug 13 '07 #8
pbmods
5,821 Expert 4TB
Heya, 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.
Aug 13 '07 #9
realin
254 100+
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 :)
Aug 13 '07 #10
pbmods
5,821 Expert 4TB
Heya, 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.
Aug 13 '07 #11
nathj
938 Expert 512MB
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
Aug 13 '07 #12
realin
254 100+
Cheers and all the best with the rest of the project.
nathj

thanks once again for wishing me luck :)
Aug 13 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gator | last post by:
Hi All, The problem is as following. Porting C++ code to C#. C++ is using several typedefs. For single .cs file there is no problem, I am using C# using keyword to declare the type name. However...
16
by: Elad | last post by:
Hi, I have an application that is made up of several executables. I need all these executables to use the same instance of an object. What is the best, most efficient way to approach this? ...
1
by: Mark Denardo | last post by:
Ok here's my problem: I have a bunch of Classes at the same namespace level say "abc.xyz". And all Classes reside in different files. abc.xyz.Class1 (in Class1.vb) abc.xyz.Class2 (in...
1
by: erictham115 | last post by:
hi Suppose i have a template class template <class V, class I=int, class S=FullMatrix<V where the FullMatrix is another template object. class matrix I will like to either create a...
2
by: prasenjit2007 | last post by:
Hello, can u help me sending Email with attachments using the Class phpMailer. On the website I have a link to an html form in which I input the parameters with the names 1)from(textbox name)...
13
by: trpost | last post by:
I am looking for a way to send data from one page to another as POST data without using forms or cURL. I have a php script that is passing a list of cases from on page to another when a link is...
12
by: Duggi | last post by:
I used to wonder why MS implemented C# to accept the following code using (Font f = new Font()) { // some code here. } While the same can be achieved through {
9
by: genesistr | last post by:
Hi, I have Image.js and Main.js files. I create image object like test = new Image(...); and it can be seen on screen but when i try to do call Move method like test.Move(x,y); it crashes. Is...
2
by: Vikas Paliwal | last post by:
Hello All, I am trying to apply tooltip class in my code in insertdata.php, but its not working can anyone help me In this code tooltip class ie 'cont tooltip ui-corner-all' properly worked my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.