Heya all,
Thanks for viewing my thread! I have a very interesting question and wonder if anyone ever came across the same question.
Say for instance I have a real large file with a class in it and the class contains 40 methods. Lets make the file 3 MB for this example. I only use a few of those methods on each page load, which makes the 3 MB page load a bit hectic.
Is there a solution to only load the specific methods from this class/file perhaps? Or is there no other way but to load the large file in its full on each page load.
I have measured that it slows down the page dramatic even if I use only 1 method from this class because of the extremely large class size.
If there is no solution to only parse the required methods into the memory, what would be the best solution?
a. Split class up in classes and files and only require them as they are needed.
What would be the best solution to write truly efficient code?
Thank you very much in advance.Mmm... found this to be quite interesting, this might just be the solution !
<!-- m --><a class="postlink" href="http://www.php.net/manual/en/language.oop5.autoload.php">http://www.php.net/manual/en/language.oop5.autoload.php</a><!-- m -->
Please correct me if I am wrong.Sounds like you've got too many methods in the class anyway (and 3MB for forty methods in one class doesn't sound very realistic, but whatever). Are you sure you're not confounding several classes together when they don't need to be? The fact that you so often need so few methods would suggest this.
I just looked at one project I have cooking here and the largest file is 81kB and has three classes in it (one 2k-line, 34-method beast and three ancillaries). The larger class is 21kB and I'm just having a hard time picturing a class that's nearly a hundred and fifty times bigger with a good reason for it. So it still doesn't sound realistic.Sounds like you've got too many methods in the class any way (and 3MB for forty methods in one class doesn't sound very realistic, but whatever). Are you sure you're not confounding several classes together when they don't need to be? The fact that you so often need so few would suggest this.
I just looked at one project I have cooking here and the largest file is 81kB and has three classes in it (one 2k-line, 34-method beast and three ancillaries). The larger class is 21kB and I'm just having a hard time picturing a class that's nearly a hundred and fifty times bigger with a good reason for it.
Hehe, yea it sound hectic right, the above is just an example, in real life this would not be, I just tried to figure out if there was a way of loading specific methods from a class file, seeing that PHP loads the whole file into memory even if it wont be using a method, I was wondering if there was a more effective way.
But logicly there wont be, so it would be more efficient to split the big class up in smaller classes, then call them as needed with the example link above for simplicity. This prevents you from including the class files the whole time, you can simply create an instance of the class.seeing that PHP loads the whole file into memory even if it wont be using a method, I was wondering if there was a more effective way. Question for you to answer: if PHP does need to use a method, what would it have to do?Question for you to answer: if PHP does need to use a method, what would it have to do?
Mmm, good point, it just could not work. But thanks for your help, it was just a though.Check the manual. PHP requires all methods for a given class to be in the same file. No way around it.Just a thought: perhaps you could create a "just the basics" class with the most commonly used methods, then create separate class (in a separate file) that extends that first class and adds all the other methods for when a more detailed implementation is needed?__call() magical method might be useful in this situation. j.a.t.Here's a rude trick I concocted some time back to pull in a class only when needed:
class aClass {
//...
function aMethod()
{
require_once('supportingClass.php');
supportingClass::supportingMethod();
}
...
}
The real one was a little more elaborate (the full story is here (<!-- m --><a class="postlink" href="http://www.phpbuilder.com/board/showthread.php?t=10253168&postid=10474602">http://www.phpbuilder.com/board/showthr ... d=10474602</a><!-- m -->)).Ah, there you have it! So it can be done.
I have tested it, and it works like a charm. Here is what I did;
I made 2 files that called two classes and two methods each, in the one class file I filled the one method with junk until I reached 3MB, the other method I made simple and very empty (this is the method I call). The other class file I did the same but I did your trick the junk was now in a supporting method in another file.
When I didnt even call the method with the junk in only the second simple method, the file loaded slow. The other one which included your trick, was immediate, so guess what, this is the solution.
Here's a rude trick I concocted some time back to pull in a class only when needed:
class aClass {
//...
function aMethod()
{
require_once('supportingClass.php');
supportingClass::supportingMethod();
}
...
}
The real one was a little more elaborate (the full story is here (<!-- m --><a class="postlink" href="http://www.phpbuilder.com/board/showthread.php?t=10253168&postid=10474602">http://www.phpbuilder.com/board/showthr ... d=10474602</a><!-- m -->)).
@ NogDog, this is a great solution in certain situations as well!
@ Qsan, what a great feature I did not even know of, well worth checking out.
Thanks for viewing my thread! I have a very interesting question and wonder if anyone ever came across the same question.
Say for instance I have a real large file with a class in it and the class contains 40 methods. Lets make the file 3 MB for this example. I only use a few of those methods on each page load, which makes the 3 MB page load a bit hectic.
Is there a solution to only load the specific methods from this class/file perhaps? Or is there no other way but to load the large file in its full on each page load.
I have measured that it slows down the page dramatic even if I use only 1 method from this class because of the extremely large class size.
If there is no solution to only parse the required methods into the memory, what would be the best solution?
a. Split class up in classes and files and only require them as they are needed.
What would be the best solution to write truly efficient code?
Thank you very much in advance.Mmm... found this to be quite interesting, this might just be the solution !
<!-- m --><a class="postlink" href="http://www.php.net/manual/en/language.oop5.autoload.php">http://www.php.net/manual/en/language.oop5.autoload.php</a><!-- m -->
Please correct me if I am wrong.Sounds like you've got too many methods in the class anyway (and 3MB for forty methods in one class doesn't sound very realistic, but whatever). Are you sure you're not confounding several classes together when they don't need to be? The fact that you so often need so few methods would suggest this.
I just looked at one project I have cooking here and the largest file is 81kB and has three classes in it (one 2k-line, 34-method beast and three ancillaries). The larger class is 21kB and I'm just having a hard time picturing a class that's nearly a hundred and fifty times bigger with a good reason for it. So it still doesn't sound realistic.Sounds like you've got too many methods in the class any way (and 3MB for forty methods in one class doesn't sound very realistic, but whatever). Are you sure you're not confounding several classes together when they don't need to be? The fact that you so often need so few would suggest this.
I just looked at one project I have cooking here and the largest file is 81kB and has three classes in it (one 2k-line, 34-method beast and three ancillaries). The larger class is 21kB and I'm just having a hard time picturing a class that's nearly a hundred and fifty times bigger with a good reason for it.
Hehe, yea it sound hectic right, the above is just an example, in real life this would not be, I just tried to figure out if there was a way of loading specific methods from a class file, seeing that PHP loads the whole file into memory even if it wont be using a method, I was wondering if there was a more effective way.
But logicly there wont be, so it would be more efficient to split the big class up in smaller classes, then call them as needed with the example link above for simplicity. This prevents you from including the class files the whole time, you can simply create an instance of the class.seeing that PHP loads the whole file into memory even if it wont be using a method, I was wondering if there was a more effective way. Question for you to answer: if PHP does need to use a method, what would it have to do?Question for you to answer: if PHP does need to use a method, what would it have to do?
Mmm, good point, it just could not work. But thanks for your help, it was just a though.Check the manual. PHP requires all methods for a given class to be in the same file. No way around it.Just a thought: perhaps you could create a "just the basics" class with the most commonly used methods, then create separate class (in a separate file) that extends that first class and adds all the other methods for when a more detailed implementation is needed?__call() magical method might be useful in this situation. j.a.t.Here's a rude trick I concocted some time back to pull in a class only when needed:
class aClass {
//...
function aMethod()
{
require_once('supportingClass.php');
supportingClass::supportingMethod();
}
...
}
The real one was a little more elaborate (the full story is here (<!-- m --><a class="postlink" href="http://www.phpbuilder.com/board/showthread.php?t=10253168&postid=10474602">http://www.phpbuilder.com/board/showthr ... d=10474602</a><!-- m -->)).Ah, there you have it! So it can be done.
I have tested it, and it works like a charm. Here is what I did;
I made 2 files that called two classes and two methods each, in the one class file I filled the one method with junk until I reached 3MB, the other method I made simple and very empty (this is the method I call). The other class file I did the same but I did your trick the junk was now in a supporting method in another file.
When I didnt even call the method with the junk in only the second simple method, the file loaded slow. The other one which included your trick, was immediate, so guess what, this is the solution.
Here's a rude trick I concocted some time back to pull in a class only when needed:
class aClass {
//...
function aMethod()
{
require_once('supportingClass.php');
supportingClass::supportingMethod();
}
...
}
The real one was a little more elaborate (the full story is here (<!-- m --><a class="postlink" href="http://www.phpbuilder.com/board/showthread.php?t=10253168&postid=10474602">http://www.phpbuilder.com/board/showthr ... d=10474602</a><!-- m -->)).
@ NogDog, this is a great solution in certain situations as well!
@ Qsan, what a great feature I did not even know of, well worth checking out.