PHP中不允许抽象特征方法是静态的吗? [英] Abstract trait't method not allowed to be static in PHP?

查看:78
本文介绍了PHP中不允许抽象特征方法是静态的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的例子:

trait FileConfig {
    public static function getPathForUploads() {
        $paths = static::getPaths();
        //etc.
    }

    abstract public static function getPaths(); //doesn't work. Error: "Static function SharedDefaultConfig::getPaths() should not be abstract"

    abstract public function getPaths(); //OK
    public static function getPaths() {} //OK
}

班级:

class AppConfig {
    use FileConfig;

    public static function getPaths() {
        return array(...);  
    }
}

致电:

AppConfig::getPathForUploads();

有必要使其静态和抽象化(强制使用 FileConfig 的类来实现 getPaths ).

我想知道如何实现更改其 static 属性的方法吗?是好的做法还是有更好的解决方案?有一天会变得违法吗?

谢谢

解决方案

此问题已在php 7中修复,因此以下代码可以正常工作:

<?php

error_reporting(-1);

trait FileConfig {
    public static function getPathForUploads() {
        echo static::getPaths();
    }

    abstract static function getPaths();
}

class AppConfig {
    use FileConfig;

    protected static function getPaths() {
        return "hello world";
    }
}

AppConfig::getPathForUploads();

http://sandbox.onlinephpfunctions.com/code/610f3140b056f3c3e8defb84e6b57>

但是在编译过程中,它实际上并不检查AppConfig中的方法是否是静态的.当您尝试静态调用非静态方法时,只会收到警告: http://sandbox.onlinephpfunctions .com/code/1252f81af34f71e901994af2531104d70024a685

Here is my example:

trait FileConfig {
    public static function getPathForUploads() {
        $paths = static::getPaths();
        //etc.
    }

    abstract public static function getPaths(); //doesn't work. Error: "Static function SharedDefaultConfig::getPaths() should not be abstract"

    abstract public function getPaths(); //OK
    public static function getPaths() {} //OK
}

Class:

class AppConfig {
    use FileConfig;

    public static function getPaths() {
        return array(...);  
    }
}

Call:

AppConfig::getPathForUploads();

It's nessessary to make it static and abstract (to force classes using FileConfig to implement getPaths).

I wonder how is it possible to implement method changing it's static property? Is it a good practice or there are better solutions? Will it one day become illegal?

Thank you

解决方案

This is fixed in php 7, so the following code works:

<?php

error_reporting(-1);

trait FileConfig {
    public static function getPathForUploads() {
        echo static::getPaths();
    }

    abstract static function getPaths();
}

class AppConfig {
    use FileConfig;

    protected static function getPaths() {
        return "hello world";
    }
}

AppConfig::getPathForUploads();

http://sandbox.onlinephpfunctions.com/code/610f3140b056f3c3e8defb84e6b57ae61fbafbc9

But it does not actually check if the method in AppConfig is static or not during compilation. You will only get a warning when you try to call the non-static method statically: http://sandbox.onlinephpfunctions.com/code/1252f81af34f71e901994af2531104d70024a685

这篇关于PHP中不允许抽象特征方法是静态的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆