PHP新静态($variable)

PHP新静态($variable),php,static,Php,Static,所有这些都在类中的一个方法中,我试图从技术上理解这段代码的作用。我在谷歌世界里到处跑。但我找不到任何答案。这只是另一种说法吗 $model = new static($variable); 还有这个呢 $model = new static $variable; 这是否意味着我正在初始化一个变量,并将其值设置为null,但我只是在运行该方法后保留该变量,以避免丢失该值?关键字new用于创建已定义类的对象 $model=新的静态($variable) 因此,这里有一个创建的model对象,

所有这些都在类中的一个方法中,我试图从技术上理解这段代码的作用。我在谷歌世界里到处跑。但我找不到任何答案。这只是另一种说法吗

 $model = new static($variable);
还有这个呢

 $model = new static $variable;

这是否意味着我正在初始化一个变量,并将其值设置为null,但我只是在运行该方法后保留该变量,以避免丢失该值?

关键字new用于创建已定义类的对象

$model=新的静态($variable)


因此,这里有一个创建的model对象,它是类static的实例,在本例中表示当前对象范围。它用于后期静态绑定

通常这与使用
self
相同。它的不同之处在于,当您拥有对象继承权时,对作用域的引用在父对象上定义,但在子对象上被调用。在这种情况下,self将引用parents范围,而static将引用孩子的

 $model = new static;

最容易将self视为定义范围,将static视为当前对象范围

self
只是它所在类的“快捷方式名称”
static
是它较新的近亲,它总是引用当前类。也就是说,当扩展一个类时,
static
如果从子上下文调用,也可以引用子类

newstatic
只意味着创建当前类的新实例,它只是
newself
的更动态的近亲


是的,
static
==更具动态性是很奇怪的。

您可以使用
newstatic()
从类内实例化一组类对象,并使其与类的扩展一起工作

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

您必须将它放在类的上下文中,
static
是对调用它的类的引用。我们可以选择将
$variable
作为参数传递给正在创建的实例的
\u构造
函数

像这样:

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
class-myClass{
私人$variable1;
公共函数构造($variable2){
$this->variable1=$variable2;
}
公共静态函数实例化器(){
$variable3=‘某些参数’;

$model=newstatic($variable3);//阅读。请查看以下链接,了解有关newstatic()的信息.我不认为静态是类,它是可变范围…是我错了还是我错了?在我寻找更多知识的过程中,我最近研究了这种晚期的静态绑定狂热,我可以诚实地说,它伤害了我的大脑。它并没有那么可怕,真的。它和<代码>$一直以来的行为一样,只是针对类而不是对象。我认为这是因为e、 正如你在最后一行中所说的,静态实际上是动态的,它让我完全失去了感觉:)我只记得
static
在本例中代表“后期静态绑定”,一切都是正确的
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;
    }
}