36

What's the difference between these object callings?

Non Static:

$var = new Object;
$var->function();

Static:

$var = User::function();

And also inside a class why should I use the static property for functions?

example:

static public function doSomething(){
    ...code...
}
Adam Halasz
  • 51,803
  • 63
  • 138
  • 208
  • 28
    @mario. A little harsh. Maybe cirk did read the manual and didn't quite grasp the concept. Seems fair to ask fellow programmers for some input. – Ben Dec 05 '10 at 22:58
  • 2
    @Ben. Maybe too harsh. But I didn't want to downvote without comment or just an *RTFM* link. And my guess is that he actually did read about it somewhere, but asked for "a second opinion" here. Which would be more ok, if only he had said so. – mario Dec 05 '10 at 23:31

5 Answers5

41

Static functions, by definition, cannot and do not depend on any instance properties of the class. That is, they do not require an instance of the class to execute (and so can be executed as you've shown without first creating an instance). In some sense, this means that the function doesn't (and will never need to) depend on members or methods (public or private) of the class.

Mark Elliot
  • 68,728
  • 18
  • 135
  • 157
  • 4
    What is the point of placing static functions inside a class instead of in, say, a separate functions.php file? – billmalarky Apr 19 '13 at 12:46
  • 11
    @billmalarky It's not essential but it at least helps you organize your functions into a file where they are relevant. To me, it's easier and more logical to have a function such as `User::someFunction();` rather than `Function::someUserFunction;` and certainly better than `someFunction();` – Britic Jul 03 '13 at 16:35
11

Difference is in the variable scope. Imagine you have:

class Student{
    public $age;
    static $generation = 2006;

   public function readPublic(){
       return $this->age;  
   }

   public static function readStatic(){
       return $this->age;         // case 1
       return $student1->age;    // case 2 
       return self::$generation;    // case 3 

   }
}

$student1 = new Student();
Student::readStatic();
  1. You static function cannot know what is $this because it is static. If there could be a $this, it would have belonged to $student1 and not Student.

  2. It also doesn't know what is $student1.

  3. It does work for case 3 because it is a static variable that belongs to the class, unlike previous 2, which belong to objects that have to be instantiated.

Cornelius
  • 303
  • 5
  • 15
8

Static methods and members belong to the class itself and not to the instance of a class.

code_burgar
  • 11,153
  • 4
  • 32
  • 52
7

Static functions or fields does not rely on initialization; hence, static.

Robin Orheden
  • 2,634
  • 20
  • 23
1

Questions regarding STATIC functions keep coming back.

Static functions, by definition, cannot and do not depend on any instance properties of the class. That is, they do not require an instance of the class to execute (and so can be executed. In some sense, this means that the function doesn't (and will never need to) depend on members or methods (public or private) of the class.

class Example {

    // property declaration
    public $value = "The text in the property";

    // method declaration
    public function displayValue() {
        echo $this->value;
    }

    static function displayText() {
        echo "The text from the static function";
    }
}


$instance = new Example();
$instance->displayValue();

$instance->displayText();

// Example::displayValue();         // Direct call to a non static function not allowed

Example::displayText();
Vincent
  • 4,076
  • 1
  • 36
  • 37