简体   繁体   English

关于PHP名称空间的几个问题

[英]Few questions about PHP namespaces

I'm porting an existing project to namespaces and i ended up hitting some walls and the official documentation could not help me. 我将现有项目移植到名称空间,最终碰到了一些麻烦,而官方文档却无济于事。 Maybe you guys can! 也许你们可以! :) :)

1) How do i extend a class from a level above in the namespace? 1)如何从名称空间中的上一级扩展类? For example: 例如:

namespace MyProject\Exceptions;

abstract class Handler {

}

namespace MyProject\Exceptions\Handlers;

class Test extends \MyProject\Exceptions\Handler {

}

2) How do i use namespaces with functions that expect callbacks? 2)如何将名称空间与需要回调的函数一起使用?

namespace MyProject;

class Main {
    public function __construct() {
        set_error_handler(array('MyProject', 'handleErrors'));
    }

    public function handleErrors() {
        echo "hi";
    }
}

Question 1: There is no shorthand for addressing the namespace "above" your current namespace. 问题1:解决当前名称空间“上方”的名称空间没有捷径。 You have to use the full path, as you already did above. 您必须像上面已经做过的那样使用完整路径。

Question 2: It is true that you can use anonymous functions (which were introduced in 5.3), and that may often be a good choice: 问题2:确实可以使用匿名函数(在5.3中引入),这通常是一个不错的选择:

namespace MyProject;

class Main {
     public function __construct() {
         set_error_handler( function() { 
              echo "hi"; 
         } );
     }
}

However, it is not always better to use anonymous functions. 但是,使用匿名函数并不总是更好。 Functions are a useful abstraction for the very reason that you can call them from different places. 函数是有用的抽象,因为您可以从不同的地方调用它们。 Anonymous functions are a shorthand for creating functions that are only needed once. 匿名函数是创建只需要一次的函数的简写。

If you want to use a regular non-anonymous function, please note first that in your above example, you are using an instance method, so you must provide an instance with $this: 如果要使用常规的非匿名函数,请首先注意在上面的示例中,您正在使用实例方法,因此必须为实例提供$ this:

namespace MyProject;

class Main {
     public function __construct() {
         set_error_handler(array($this, 'handleErrors'));
     }

     public function handleErrors() {
         echo "hi";
     }
}

Additionally, there is the case where you want to address a static function from a namespaced class. 此外,在某些情况下,您想从命名空间类访问静态函数。 In this case, the array syntax must include the class name. 在这种情况下,数组语法必须包含类名。 If the class name has a namespace, you should use the full namespace path as you would in your source code: 如果类名具有名称空间,则应像在源代码中那样使用完整的名称空间路径:

namespace MyProject;

class Main {
    public function __construct() {
        set_error_handler(array('MyProject\Main', 'handleErrors'));
    }

    public static function handleErrors() {
        echo "hi";
    }
}

Just like other namespace things. 就像其他名称空间一样。 However, make sure to escape the backslashes! 但是,请确保不要使用反斜杠!

-edit- -编辑-

Since we're talking about "modern PHP" here, I strongly recommend simply passing a function as a callback rather than using your way of building a callback. 由于我们在这里谈论“现代PHP”,因此我强烈建议您简单地将函数作为回调传递,而不要使用构建回调的方式。 Nowadays you can simply accept a function and use that as the callback, as you can do in other programming languages such as C++. 如今,您可以像接受其他编程语言(如C ++)那样简单地接受一个函数并将其用作回调。

namespace Foo;
function Bar($callback) { $callback(); }

// Other file
Foo\Bar(function() {
    // Do something
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM