10
 $model = new static($variable);

All these are within a method inside a class, I am trying to technically understand what this piece of code does. I ran around in the Google world. But can't find anything that leads me to an answer. Is this just another way of saying.

 $model = new static $variable;

Also what about this

 $model = new static;

Does this mean I'm initializing a variable and settings it's value to null but I am just persisting the variable not to lose the value after running the method?

Some User
  • 181
  • 1
  • 8

5 Answers5

12

static in this case means the current object scope. It is used in late static binding.

Normally this is going to be the same as using self. The place it differs is when you have a object heirarchy where the reference to the scope is defined on a parent but is being called on the child. self in that case would reference the parents scope whereas static would reference the child's

class A{
    function selfFactory(){
        return new self();
    }

    function staticFactory(){
        return new static();
    }
}

class B extends A{
}


$b = new B();

$a1 = $b->selfFactory(); // a1 is an instance of A

$a2 = $b->staticFactory(); // a2 is an instance of B

It's easiest to think about self as being the defining scope and static being the current object scope.

halfer
  • 19,824
  • 17
  • 99
  • 186
Orangepill
  • 24,500
  • 3
  • 42
  • 63
4

self is simply a "shortcut name" for the class it occurs in. static is its newer late static binding cousin, which always refers to the current class. I.e. when extending a class, static can also refer to the child class if called from the child context.

new static just means make new instance of the current class and is simply the more dynamic cousin of new self.

And yeah, static == more dynamic is weird.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • In my search for more knowledge I recently looked into this late static binding craziness, and I can honestly say, It hurts my brain. – Dale Jun 07 '13 at 07:02
  • It's not that terrible, really. It's the same `$this` has always behaved, just for classes instead of objects. – deceze Jun 07 '13 at 07:04
  • I think it's because, as you say in the last line, static is actually dynamic, it just throws me right off the scent :) – Dale Jun 07 '13 at 07:09
  • 1
    I just remember that `static` in this case stands for "late *static* binding", and all is right again. :) – deceze Jun 07 '13 at 07:12
2

You have to put it in the context of a class where static is a reference to the class it is called in. We can optionally pass $variable as a parameter to the __construct function of the instance you are creating.

Like so:

class myClass {

    private $variable1;

    public function __construct($variable2) {
        $this->variable1 = $variable2;
    }

    public static function instantiator() {
        $variable3 = 'some parameter';
        $model = new static($variable3); // <-this where it happens.
        return $model;
    }
}

Here static refers to myClass and we pass the variable 'some parameter' as a parameter to the __construct function.

wranvaud
  • 1,073
  • 13
  • 18
0

You can use new static() to instantiate a group of class objects from within the class, and have it work with extensions to the class as well.

class myClass {
  $some_value = 'foo';
  public function __construct($id) {
    if($this->validId($id)) {
      $this->load($id); 
    }
  }

  protected function validId($id) {
    // check if id is valid.
    return true; // or false, depending
  }

  protected function load($id) {
    // do a db query and populate the object's properties
  }

  public static function getBy($property, $value) {
    // 1st check to see if $property is a valid property
    // if yes, then query the db for all objects that match
    $matching_objects = array();
    foreach($matching as $id) {
      $matching_objects[] = new static($id); // calls the constructor from the class it is called from, which is useful for inheritance.
    }
    return $matching_objects;
  }
}


myChildClass extends myClass {
  $some_value = 'bar'; // 
}


$child_collection = myChildClass::getBy('color','red'); // gets all red ones

$child_object = $child_collection[0];

print_r($child_object); // you'll see it's an object of myChildClass
Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120
-2

The keyword new is used to make an object of already defined class

$model = new static($variable);

so here there is an object of model created which is an instance of class static

mmratxs
  • 86
  • 2
  • 9