PHP new static (variable $)

$model = new static($variable); 

All this inside a method inside a class, I am trying to technically understand what this piece of code does. I ran in the world of Google. But I can not find anything that would lead me to an answer. This is just another way of saying.

  $model = new static $variable; 

And what about this

  $model = new static; 

Does this mean that I initialize the variable and set its value to null, but I just save this variable so as not to lose the value after the method starts?

+8
php static
source share
6 answers

static in this case means the area of ​​the current object. It is used in late static binding.

This will usually be the same as using self . The place where it differs is when you have a hierarchy of objects where the reference to the region is determined by the parent, but called on the child. self in this case will refer to the area with parents, while static will refer to the child

 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 

The easiest way to think of yourself as a defining sphere and static, which is the region of the current object.

+8
source share

Please take a look at the following link to get an idea of ​​the new static ().

New me versus new static

+2
source share

self is just a shortcut to the class in which it occurs. static is its new late cousin static binding , which always refers to the current class. That is, when extending a class, static can also refer to a child class if it is called from a child context.

new static simply means creating a new instance of the current class and just a more dynamic cousin from new self .

And yes, static == is more dynamically weird.

+2
source share

You must specify it in the context of the class, where static is a reference to the class in which it is called. We can optionally pass $variable as a parameter to the __construct function of the instance that you use Create.

Same:

 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.

+1
source share

You can use new static() to create an instance of a group of class objects from a class, and also work with extensions for this class.

 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 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 an object of myChildClass 
0
source share

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

$ model = new static (variable $);

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

-2
source share

All Articles