Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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
Php 为什么静态调用接口属性并作为参数传递_Php_Oop - Fatal编程技术网

Php 为什么静态调用接口属性并作为参数传递

Php 为什么静态调用接口属性并作为参数传递,php,oop,Php,Oop,我最近遇到了一些PHP,其中在接口中定义了常量,然后静态调用这些常量并将其传递给实现该接口的对象中的方法 这有什么用呢?我猜传递到该方法中的值将在一个地方更新/更改,但是没有办法通过在该方法参数上的类型暗示来强制执行该操作,以便传递任何东西 例如: interface test_inter { const foo = "bar" } class test_obj implements test_inter { public function test_func( string $tes

我最近遇到了一些PHP,其中在接口中定义了常量,然后静态调用这些常量并将其传递给实现该接口的对象中的方法

这有什么用呢?我猜传递到该方法中的值将在一个地方更新/更改,但是没有办法通过在该方法参数上的类型暗示来强制执行该操作,以便传递任何东西

例如:

interface test_inter {
  const foo = "bar"
}

class test_obj implements test_inter {
  public function test_func( string $test_param ) {
  }
}

$obj = new test_obj();
$obj->test_func(test_inter::foo);

可能有一些语法错误,因为我刚刚作为演示键入了它,我的问题更多的是为什么会实现(甚至应该?)这样的东西,而不是上面示例中的任何小错误。

它允许您定义每个实现将共享和使用的值/常量,例如:

interface HttpRequest {
    const GET  = 1;
    const POST = 2;
    ...

    public function makeRequest($type);
}

...

$req = new RequestImpl();
$req->makeRequest(HttpRequest::POST);

任何答案基本上都是意见的问题,这在本论坛的主题上通常不是