5

When should I use Static functions/classes/fields in PHP? What are some practical uses of it?

1
  • 3
    If you are trying to write object oriented code: NEVER. Commented Aug 11, 2012 at 21:57

3 Answers 3

5

you should not, it's rarely useful. common usage for statics are factory methods and singleton::instance()

factory:

class Point{ private $x; private $y; public function __construct($x, $y){ ... } static function fromArray($arr){ return new Point($arr["x"], $arr["y"]); } } 

singleton:

class DB{ private $inst; private function __construct(){ ... } static function instance(){ if ($this->inst) return $this->inst; return $this->inst = new DB(); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

I agree it's mostly not necessary and harmful. I would add one case where it is usefull: to create a higher level language. Eg User::with100Points(). More can be found in the book Growing object oriented software guided by tests.
@koen explain a bit more, how would User::with100points() work?
@Click Upvote If you mean how it looks in code: class User { public function setPoints($points) { //set points } public static function with100points() { return new self(100); } }. User::with100points() is more readable than $user = new User(); $user->setPoints(100); You could create a special class for this DSLUser extends User { public static method with100points() {} }. There aren't that many opportunities to use this correctly in your code but in UnitTests you can make more use of it and your tests often become very clear.
3

Usage of static methods in same in languages like Java/PHP.

One simple example can be that you want to use a variable across all instances of your class and any instance can change its value and you want it to get reflected in other instance as well.

 class Foo{ static $count=0; public function incrementCount(){ self::$count++; } public function getCount(){ return self:$count; } } 

Without static you can't set count value via one object and access it in others.

Comments

1

I occasionally use STATIC Methods when I need simple functions in a Class that I also use outside the Class such as:

in a UserProfile class I have an method that returns an array that is used to pass data back to the class after the array is populated from the html page.

 Class UserProfile{ Public Static get_empty_array(){ return array('firstname'=>'',lastname=>''); //usually much more complex multi-dim arrays } } 

This way the empty array can be used within the class/object and outside as a starting template. I also use Static Methods for functions that would normally be standalone functions but I want to keep them in the class so it is all together but also make them available outside as a static method such as:

 public static convert_data($string){ //do some data conversion or manipulating here then return $ret_value; } $converted_data = class::convert_data($string); 

I do maintain an library of common user defined functions but I have found it handy to include some in the class to which it is closely related.

2 Comments

Kind of like namespacing your global functions.
Another way to put it: if the method doesn't influence the state of the object, it's a good candidate for a static method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.