Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP7.1中指出返回类型是当前的子类型?_Php_Php 7_Type Hinting_Php 7.1_Late Static Binding - Fatal编程技术网

如何在PHP7.1中指出返回类型是当前的子类型?

如何在PHP7.1中指出返回类型是当前的子类型?,php,php-7,type-hinting,php-7.1,late-static-binding,Php,Php 7,Type Hinting,Php 7.1,Late Static Binding,我有 $x没有类型提示我喜欢类型提示,所以我想要B的类型提示,而不是A 如何为$x直接启用类型提示 我的想法是这样的 abstract class A{ public static function getSingle($where = []) { $classname = get_called_class(); $record = static::turnTheWhereIntoArecordFromDB($where); $model = new $c

我有

$x
没有类型提示我喜欢类型提示,所以我想要
B
的类型提示,而不是
A

如何为
$x
直接启用类型提示

我的想法是这样的

abstract class A{
  public static function getSingle($where = []) {
    $classname = get_called_class();

    $record =     static::turnTheWhereIntoArecordFromDB($where);
    $model = new  $classname($record);
    return $model;
  }
}

class B extends A{

}


$x = B::getSingle();
这显然行不通


有什么办法吗?

@method B getSingle
添加到
B
类phpdoc中

 public function getSingle($where = []) : ?get_called_class()

对于您介绍的示例,为什么需要工厂方法?您正在从构造函数创建一个新实例,为什么不干脆
$x=new B($record)

以上更新



@return static
将键入提示其子类的内容。我还将您的函数更改为静态函数,这是典型的工厂模式。

让抽象类实现一个接口并键入该接口的提示。子级不必显式实现接口

abstract class A
{
    /**
     * @param array $where
     * @return static
     */
    public static function getSingle($where = [])
    {
        $classname = get_called_class();

        $model = new  $classname($record);
        return $model;
    }
}

我知道您指定了PHP7.1,但从PHP8开始(将于2020年11月发布)这将通过添加
静态
返回类型提示
实现

这意味着您将能够:

abstract class A implements BarInterface
  {
    public function foo (): BarInterface
      {    
        return new static;
      }
  }
此外,不需要到处乱搞
$classname=get_called_class();新的$classname()。您可以简单地执行
newstatic()
,这更整洁,并且具有相同的结果


Bar::build()
类型提示是为了表明由于后期静态绑定,它返回
Bar
,而不是
Foo

返回值类型提示有问题吗?这意味着A和所有的孩子都可以回来。这似乎有点牵强。。。想想
self
就是那种魔力所能达到的程度。不过很有趣。这些可能的类是否扩展了公共父类?实现一个公共接口?@JoshuaK我想为类B提供类型提示。@ficuscr
a
基本上是一个面向差劲程序员的ORM。例如,
save()
refresh()
delete()
等,但B将具有一些用于更高级SQL的实用功能。。。目前,仅此而已。但一切都有可能,就像我开始想的那样@Toskan这只是为了IDE糖吗?实际上这破坏了我的IDE。例如,执行
$b->getSingle()现在不再跳转到函数中,而是跳转到php文档中。它实际上是静态的,但似乎也不起作用
@method static B getSingle
似乎无法正常工作me@ficuscr正确的代码错误会更好,但是是的,IDE sugar在PhpStorm中更重要,至少代码完成是有效的。但是@bijiDango方法似乎工作得更好。很遗憾你不能做
:?static
no?我不知道你的意思。
公共静态函数getSingle($where=[]):?static
无效
公共静态函数getSingle($where=[]):?MyClass
是有效的PHP7.1语法,并强制返回类型为
MyClass
null
(由于
null是允许的),我的意思是phpdoc是可以的。但是,如果php真的在本机上支持将返回类型声明为
static
,那就更好了,因为您有自己的观点。也许有一天它会成为现实。但是如果不是为了类型暗示,我看不出
:self
:static
有什么不同。但是
B
显然会有更多的功能
abstract class A implements BarInterface
  {
    public function foo (): BarInterface
      {    
        return new static;
      }
  }
class Foo

   public static function build(): static
   {
        return new static();
   }
}

class Bar extends Foo {}