/    Sign up×
Community /Pin to ProfileBookmark

Class using another class?

Is it possible to use a class inside of another class?

I have a class that (using AJAX) interacts with a database (via PHP). Now I’m creating another class, a job ticket, that when an instance is created, must query the database to fill all the appropriate variables. My question is, is there a way to import the first class, so that it is usable in the second class, or do I have to copy and paste the class inside of the second class?

I hope all that makes sense. If anyone can offer any ideas, I’d really appreciate it!

Thanks,
Brian

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameMay 18.2009 — I have a class that (using AJAX) interacts with a database (via PHP). Now I'm creating another class, a job ticket, that when an instance is created, must query the database to fill all the appropriate variables. My question is, is there a way to import the first class, so that it is usable in the second class, or do I have to copy and paste the class inside of the second class?[/quote]


Well, without more info, I'm not sure if I understand the problem or flow correctly.. but basically what it sounds like to me is something like (going back to your earlier thread here: http://www.webdeveloper.com/forum/showthread.php?t=208619 ):

function database(host, username, password) {
this.host = host;
this.username = username;
this.password = password;
this.xhr = HttpObject();
this.result = null;
this.mysql_query = mysql_query;
}

function JobTicket() {
var getData = (function (obj,methodName) { return function () { obj[methodName](); }; })(this,'init');
this.dataConnect = new database('localhost', 'testuser', 'testpass');
this.dataConnect.mysql_query('SELECT * FROM test_table', getData);
}

JobTicket.prototype = {
init : function () {
if (this.dataConnect.result !== null) {
//process this.dataConnect.result from the ajax call,
//for example, something like: this.property1 = this.dataConnect.result.split(':::'); or however.
}
}
};


Just a vague suggestion. If this doesn't get you further along, perhaps you could post what you got, good luck!
Copy linkTweet thisAlerts:
@KorMay 18.2009 — What about giving the element [I]two[/I] classes?
Copy linkTweet thisAlerts:
@bcmannauthorMay 24.2009 — Thanks for the replies. Sorry it took me a little bit to get back to this one.

I have a class that is called "database". Then I have another class called "jobTicket". In the jobTicket class, I want to use methods from the database class, so I create an instance of the database class within the jobTicket class. What I've done (which seems to work), is in the head section of my document, I link the database class first, then link the jobTicket class. So when the jobTicket class creates an instance, it is there.

What I'm asking is, is there a way to link the database class to the jobTicket class without having to do it in the head section of each document? (In case I forget to do that). It also seems like it would be cleaner to just link the jobTicket class when it's used, and it will link up the other class automatically.

In PHP, I would use "require_once('filename');", but I can't seem to find an equivalent to that in Javascript.

Thanks again for all the help!

Brian
×

Success!

Help @bcmann spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...