Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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构建一个api,我不太熟悉扩展类或使用traits。目前我正在使用traits来更好地构造我的主API类 有关当前的工作方式,请参见下文。我想知道是否可以在API类中创建一个类。例如,负责webhook方法的人。这需要访问当前实例的路由器和api上的所有方法 示例 当人们在/v1/{method}/{verb}/{args} if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) { $_SERVER['HTTP_ORIGIN'] =

我正在用php构建一个api,我不太熟悉扩展类或使用traits。目前我正在使用traits来更好地构造我的主
API

有关当前的工作方式,请参见下文。我想知道是否可以在
API
类中创建一个类。例如,负责
webhook
方法的人。这需要访问当前实例的路由器和api上的所有方法

示例

当人们在
/v1/{method}/{verb}/{args}

if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) {
    $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME'];
}

try {
    $API = new API($_REQUEST['request'], $_SERVER['HTTP_ORIGIN']);
    echo $API->processAPI();
}

catch (Exception $e) {
    echo json_encode(['error' => $e->getMessage()]);
}
路由器类

abstract class router {
    /* 
        Methods which seek out
        * method
        * verb
        * args

        And do authentication
    */
}
我的主要API类

class API extends router {
    // some generic methods specifically for this class

    // Other methods but in different files (for readability and oversight)
    use otherMethod1;
    use otherMethod2;
    // ...
}

您可以将webhook类对象注入API类。小例子,按要求

  class Webhook { // some methods }

  class API extends router {
        private $webhook;

        public function __construct(Webhook $webhook)
        {
             $this->webhook = $webhook;
        }
        // some generic methods specifically for this class

        public function useWebhook()
        {
             $result = $this->webhook->someWebhookMethod();
             // ...
        }

        // Other methods but in different files (for readability and oversight)
        use otherMethod1;
        use otherMethod2;
        // ...
    }
当您可以像这样创建对象时

$webhook = new Webhook();
$api = new API($webhook);

$api->useWebhook();
你在找这个吗


您可以在这里阅读有关这种称为“依赖注入”的方法的更多信息:

您的问题是什么?@VladimirKovpak ikr。。。基本上,我们希望在继承实例的父函数和变量的同时多次扩展一个类。如果这更有意义的话-我不知道该怎么解释@NealVDV您应该创建webhook类的对象并将其注入API。您可以通过构造函数或setter方法传递它。@TomasJavaiš这听起来像是我在搜索的东西!你能举一个很小的例子说明如何做到这一点吗?