使用NuSoap注册命名空间的PHP类方法

问题描述 投票:2回答:1

是否可以使用NuSoap库注册PHP自动加载的命名空间类方法?要清楚,PHP类是命名空间。这不是关于XML命名空间的问题。

例如,如果我在某个文件中定义了一个类:

<?php

  namespace \My\Fancy\Namespace;

  class MyClass {
       public static function foo() { /* ... */ }
       // ...
  }

我想用我的肥皂服务器注册它:

    <?php

    use \My\Fancy\Namespace\MyClass; // autoloaded

    $server = new soap_server();

    // ... server configuration ...

    $server->register(
        'MyClass..foo'  // This does not work
        // ... etc. ...
    );

我没有运气就进行了如此多的搜索,所以我希望我不会问一个糟糕的问题。

php soap autoload nusoap
1个回答
1
投票

在invoke_method方法中的nusoap_server类中,代码不允许使用名称空间。

function invoke_method() {
    // many lines of code...

    $class = '';
    $method = '';
    if (strlen($delim) > 0 && substr_count($this->methodname, $delim) == 1) {
        $try_class = substr($this->methodname, 0, strpos($this->methodname, $delim));
        if (class_exists($try_class)) {
            // get the class and method name
            $class = $try_class;
            $method = substr($this->methodname, strpos($this->methodname, $delim) + strlen($delim));
            $this->debug("in invoke_method, class=$class method=$method delim=$delim");
        } else {
            $this->debug("in invoke_method, class=$try_class not found");
        }
    } else {
        $try_class = '';
        $this->debug("in invoke_method, no class to try");
    }

    // many lines of code...
}

但感谢上帝的开源,所以我们修改代码吧! 这是你可以做的:

function invoke_method() {
    // many lines of code...

    $class = '';
    $method = '';
    if (strlen($delim) > 0 && substr_count($this->methodname, $delim) == 1) {
        $try_class = substr($this->methodname, 0, strpos($this->methodname, $delim));
        if (class_exists($try_class)) {
            // get the class and method name
            $class = $try_class;
            $method = substr($this->methodname, strpos($this->methodname, $delim) + strlen($delim));
            $this->debug("in invoke_method, class=$class method=$method delim=$delim");
        } else {
            $this->debug("in invoke_method, class=$try_class not found");
        }
    } elseif (strlen($delim) > 0 && substr_count($this->methodname, $delim) > 1) {
        $split = explode($delim, $this->methodname);
        $method = array_pop($split);
        $class = implode('\\', $split);
    } else {
        $try_class = '';
        $this->debug("in invoke_method, no class to try");
    }

    // many lines of code...
}

解释:

// Example of namespaced class
// $this->methodname = 'Namespace.Namespace2.Class.MethodName';

// Has delimiter "." or ".." more than one time.
if (strlen($delim) > 0 && substr_count($this->methodname, $delim) > 1) {
    // code below
}

// Transform in array
$split = explode($delim, $this->methodname);
// $split = array('Namespace', 'Namespace2', 'Class', 'MethodName')

// Get the last item (method name) and remove from array.
$method = array_pop($split);
// $method = 'MethodName'
// $split = array('Namespace', 'Namespace2', 'Class')

// Transform the class name
$class = implode('\\', $split);
// $class = 'Namespace\Namespace2\Class'

通过这种简单的修改,您可以使用PHP命名空间功能。 我知道很长一段时间过去但我希望通过这个答案来帮助一个人。

© www.soinside.com 2019 - 2024. All rights reserved.