static or dynamic methods?
Posted: Tue May 19, 2009 5:06 am
I've never been so sure about the two. I know how to use them and the fact that static refers to all instances of the instantiated objects (or the entire class as general) and dynamic refers to each instantiated object individually. But I never got to find out which one to use at which situations i.e. which one would work best for specific tasks? (or HAS to be used for specific tasks)
The only good example of static usage I know is too keep track of number of instances created:
but then again if you remove the protected scope, you can access it directly and the dynamic usage becomes completly obsolete.
The only good example of static usage I know is too keep track of number of instances created:
Code: Select all
<?php
class abc {
protected static $_counter = 0;
public $num;
public function __construct(){
self::$_counter++;
$this->num = self::$_counter;
}
}
$a = new abc();
echo $a->num; // 1
$b = new abc();
echo $b->num; // 2
?>Code: Select all
<?php
echo abc::$counter;
?>