Xeoncross Posted February 7, 2008 Share Posted February 7, 2008 I am making some classes and I originally was just initializing them ($obj = new class()) then passing that obj around to functions though the "global $obj;" way of importing variables/arrays/stings/objects etc.. <?php $obj = new my_class(); function test() { //bring the obj into the scope of the function global $obj; //then use the obj $obj->method(); ... ... } ?> However, I was thinking that since these classes had no values to store I should just use them as static classes. <?php class my_class { ... ... } function test() { ... //then use the obj $obj::method(); ... } ?> Is this a better way to deal with a class that has to be used 2-10 times a script run? At least I don't have to use "global" right? I did some testing and it seems to be about .03 seconds faster for 100,000 loops. Here is the code I ran: <?php class show_content { function keep_counting() { $x=0; while($x < 10) { $x = $x + $x - $x + 1; $b = $x*1000/3; $b *= 999; show_content::bar(); } } function bar() { for($x=0;$x<100;$x++) { show_content::foo('this'); } } function foo($value) { $new_value = $value; } } $show_content = new show_content; ?> <table cellpadding="5" border="1"> <tr> <td> <?php $time_start = array_sum(explode(' ', microtime())); for($x=0;$x<100;$x++) { show_content::keep_counting(); } echo round(array_sum(explode(' ', microtime())) - $time_start, 6); ?> </td> <td> <?php $time_start = array_sum(explode(' ', microtime())); for($x=0;$x<100;$x++) { $show_content->keep_counting(); } echo round(array_sum(explode(' ', microtime())) - $time_start, 6); ?> </td> </tr> <tr> <td> <?php $time_start = array_sum(explode(' ', microtime())); for($x=0;$x<100;$x++) { show_content::keep_counting(); } echo round(array_sum(explode(' ', microtime())) - $time_start, 6); ?> </td> <td> <?php $time_start = array_sum(explode(' ', microtime())); for($x=0;$x<100;$x++) { $show_content->keep_counting(); } echo round(array_sum(explode(' ', microtime())) - $time_start, 6); ?> </td> </tr> <tr> <td> <?php $time_start = array_sum(explode(' ', microtime())); for($x=0;$x<100;$x++) { show_content::keep_counting(); } echo round(array_sum(explode(' ', microtime())) - $time_start, 6); ?> </td> <td> <?php $time_start = array_sum(explode(' ', microtime())); for($x=0;$x<100;$x++) { $show_content->keep_counting(); } echo round(array_sum(explode(' ', microtime())) - $time_start, 6); ?> </td> </tr> </table> Would someone please try this script and recommend any way to improve it to make sure I am not goofing up my tests? Quote Link to comment https://forums.phpfreaks.com/topic/89859-static-vs-created-oop/ Share on other sites More sharing options...
trq Posted February 7, 2008 Share Posted February 7, 2008 Have a read of this thread. Static classes are really no better than globals, though using the global keyword in OOP is really frowned upon. There are much better way of gaining access to your classes. Quote Link to comment https://forums.phpfreaks.com/topic/89859-static-vs-created-oop/#findComment-460702 Share on other sites More sharing options...
Xeoncross Posted February 7, 2008 Author Share Posted February 7, 2008 Have a read of this thread. Static classes are really no better than globals I think you mean they are no better than functions, as you can at least access and change a set value with a global. (talking PHP4 here) There are much better way of gaining access to your classes. After reading that thread, and almost HALF of the articles on 448191's page, I still can't figure out a better way to handle keeping a group of functions together (that don't have any shared values that I would need to preserve with an object). If you know of something please let me know as I have racked my brain reading everything I can on advanced OOP this last week. Quote Link to comment https://forums.phpfreaks.com/topic/89859-static-vs-created-oop/#findComment-461060 Share on other sites More sharing options...
Xeoncross Posted February 7, 2008 Author Share Posted February 7, 2008 Further more, after a lot of testing it seems that accessing static classes in PHP 4 takes more memory, but is faster than having one singleton object based off the same class that is called repeatedly. I ran 100,000 literation's of a call to an object->that called member function->created var. Static: 576 bytes Used 0.561687 Time Object: 320 bytes Used 0.619534 Time Based off of this it seems that if it is a large class you can save memory by created an object out of it and declaring it global. Below is my revised code which you can run. <?php print 'Memory to Start:'. memory_get_usage(). '<br />'; class stats { var $memory = null; var $start_time = null; function start() { //$this->start_time = array_sum(explode(' ', microtime())); $this->start_time = (time()+microtime()); $this->memory = memory_get_usage(); } function finish() { echo (memory_get_usage() - $this->memory). ' bytes Used<br />'; //echo round(array_sum(explode(' ', microtime())) - $this->start_time, 6). ' Time'; echo round((time()+microtime()) - $this->start_time, 6). ' Time'; $this->memory = null; $this->start_time = null; } } $stats = new stats(); class static_class { function keep_counting() { $x=0; while($x < 10) { $x = $x + $x - $x + 1; $b = $x*1000/3; $b *= 999; static_class::bar(); } } function bar() { for($x=0;$x<10;$x++) { static_class::foo($x); } } function foo($value) { $new_value = $value; } } class created_class { function keep_counting() { $x=0; while($x < 10) { $x = $x + $x - $x + 1; $b = $x*1000/3; $b *= 999; $this->bar(); } } function bar() { for($x=0;$x<10;$x++) { $this->foo($x); } } function foo($value) { $new_value = $value; } } function call_obj($static=true) { if($static) { static_class::keep_counting(); } else { global $created_class; $created_class->keep_counting(); } } ?> <table cellpadding="5" border="1"> <tr> <td> <?php $stats->start(); for($x=0;$x<1000;$x++) { static_class::keep_counting(); call_obj(); } $stats->finish(); ?> </td> </tr> <tr> <td> <?php $stats->start(); $created_class = new created_class; for($x=0;$x<1000;$x++) { $created_class->keep_counting(); call_obj(false); } $stats->finish(); ?> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/89859-static-vs-created-oop/#findComment-461068 Share on other sites More sharing options...
448191 Posted February 7, 2008 Share Posted February 7, 2008 I still can't figure out a better way to handle keeping a group of functions together (that don't have any shared values that I would need to preserve with an object). If you just want to group a number of functions a static class would work fine. The real question is, why do you need too? I'll bet every piece of behaviour you plan on putting in there actually belongs in some stateful class. Quote Link to comment https://forums.phpfreaks.com/topic/89859-static-vs-created-oop/#findComment-461125 Share on other sites More sharing options...
Xeoncross Posted February 7, 2008 Author Share Posted February 7, 2008 The real question is, why do you need too? I'll bet every piece of behaviour you plan on putting in there actually belongs in some stateful class. If so, I would love it if you could be one of the testers of the system when I am done with it. Maybe just from glancing over the logic and classes you will see something that I have missed. But at any rate, this static method, while talking more ram seems to be faster than an object and I can avoid "global $obj" so I think that I will go with it for now. The worse case scenario is that later on, I copy the methods out of this class - and place them in another objects class like you said. Quote Link to comment https://forums.phpfreaks.com/topic/89859-static-vs-created-oop/#findComment-461164 Share on other sites More sharing options...
448191 Posted February 7, 2008 Share Posted February 7, 2008 If so, I would love it if you could be one of the testers of the system when I am done with it. Maybe I will, or at least at look at it and make some concrete suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/89859-static-vs-created-oop/#findComment-461203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.