Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Ajax -> How to avoid including every class for one query

I have several classes in PHP. All separated in files. (class1.php, class2.php)

On my site, I have included them all together, so I can use all of them.

Now, when I use jQuery Ajax to get data from the database I have to call another file (like getData.php)

But on this file, I have to include all of the classes (or at least some) again. So every time I want to receive data from the database, it loads all classes again(I think). The problem is, that they're not that small and they are still growing. That's bad, I think.

Is there a way to avoid this? So that I include the classes only once.

like image 442
Quantm Avatar asked Dec 06 '25 10:12

Quantm


1 Answers

step one: only include the files you need. How? using an autoloader (and a system that can easily determine what file contains which class, like Classname.class.php or something) will help: If you call new yourClass() the autoloader will load the file, but only that file.

Now you have a minimal set of files loaded. This set will not get smaller, as you are actually using the files. What you can do is speed stuff up by

  1. using APC. Your PHP files get 'cached' and they don't need to be re-parsed again and again.
  2. use other forms of cache, like memcached: save certain data in memory so you don't need anything other then your memcache-class loaded (heck, you won't even hit your database anymore for these requests :) )
like image 179
Nanne Avatar answered Dec 07 '25 23:12

Nanne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!