Jump to content

Static vs Created OOP


Xeoncross

Recommended Posts

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? ;)

Link to comment
Share on other sites

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.  8)

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.