【问题标题】:PHP new static($variable)PHP 新静态($变量)
【发布时间】:2013-06-07 06:09:17
【问题描述】:
 $model = new static($variable);

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

 $model = new static $variable;

这个呢

 $model = new static;

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

【问题讨论】:

标签: php static


【解决方案1】:

在这种情况下,静态表示当前对象范围。它用于后期静态绑定。

通常这与使用self 相同。它不同的地方是当你有一个对象层次结构时,对范围的引用是在父级上定义的,但在子级上被调用。在这种情况下,self 将引用父母的范围,而 static 将引用孩子的

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

将 self 视为定义范围而将 static 视为当前对象范围是最简单的。

【讨论】:

    【解决方案2】:

    self 只是它所在类的“快捷方式名称”。static 是其较新的 late static binding 表亲,它始终引用 current 类。 IE。当扩展一个类时,static 如果从子上下文中调用,也可以引用子类。

    new static 只是表示为当前类创建新实例,它只是new self 的动态表亲。

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

    【讨论】:

    • 在寻找更多知识的过程中,我最近研究了这种后期的静态绑定疯狂,老实说,它伤害了我的大脑。
    • 真的没那么可怕。这与 $this 一直以来的行为相同,只是针对类而不是对象。
    • 我认为这是因为,正如你在最后一行所说的,静态实际上是动态的,它只是让我立刻失去了气味:)
    • 我只记得在这种情况下static 代表“后期静态 绑定”,一切正常。 :)
    【解决方案3】:

    您必须将它放在一个类的上下文中,其中static 是对调用它的类的引用。我们可以可选地$variable 作为参数传递给@987654323 @你正在创建的实例的函数。

    像这样:

    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;
        }
    }
    

    这里static 指的是myClass,我们将变量“一些参数”作为参数传递给__construct 函数。

    【讨论】:

      【解决方案4】:

      您可以使用new static() 从类中实例化一组类对象,并使其与类的扩展一起使用。

      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
      

      【讨论】:

        【解决方案5】:

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

        $model = new static($variable);

        所以这里创建了一个模型对象,它是静态类的一个实例

        【讨论】:

        • 我不认为静态是类,它是变量范围...是我错了吗?
        猜你喜欢
        • 2010-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-09
        相关资源
        最近更新 更多