PHP命名空间覆盖Use语句 [英] PHP Namespaces Override Use Statement

查看:249
本文介绍了PHP命名空间覆盖Use语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我是否可以覆盖use语句吗?

Can anyone tell me if it's possible to override a use statement?

我的示例有一个MVC设置,其中有核心代码,能够使用扩展核心版本的自定义版本覆盖每个控制器/模型.

My example is having an MVC setup where there is core code with the ability to override each Controller / Model with a custom version that extends the core version.

我要面对的问题是我的核心控制器有一条use语句,告诉它使用核心模型,因此,如果我扩展该模型,我不确定如何告诉它使用自定义模型而不是核心模型.

The issue I face is that my core controller has a use statement telling it to use the core model, so if I extend the model, I'm not sure how to tell it to use the custom model rather than the core one

我显然可以更新核心控制器use语句以指向自定义代码,但是核心代码是共享的,因此自定义版本可能在使用该核心内核的其他网站上不存在

I could obviously update the core controller use statement to point to the custom one, but the core code is shared so the custom version may not exist on other sites that use this core core

使用语句显然是文件级的,所以我猜测这是不可能的,但是我希望有一些我不知道的东西或者一种解决方法

Use statements are obviously file level so I'm guessing it's not possible, but I'm hoping there's either something I don't know about or maybe a workaround

示例

核心控制器

namespace Core;

use Core\Model\Example as ExampleModel;

class ExampleController {

    public function output() {
        $model = new ExampleModel;
        $model->test();
    }

}

核心模型

namespace Core;

class ExampleModel() {

    public function test() {
        echo 'This is the core test';
    }

}

自定义控制器

namespace Custom;

use Custom\Controller\Example as Base,
    Custom\Model\Example as ExampleModel;

class ExampleController extends Base {

    //Inherits the output() method

}

自定义模型

namespace Custom;

use Core\Model\Example as Base;

class ExampleModel extends Base {

    public function test() {
        echo 'This is the custom test';
    }

}

因此,给出这个示例,是否可以创建一个使用自定义模型输出这是自定义测试"的自定义控制器的实例,而无需完全修改核心代码?

So given this example, is it possible for me to create an instance of the custom controller which uses the custom model to output 'This is the custom test', without modifying the core code at all?

希望我的要求是有道理的

Hopefully what I'm asking makes sense

谢谢

推荐答案

我不太确定我是否理解您的问题,但是答案应该是不言而喻的:如果您的自定义模型是从核心模型扩展的,则可以从该自定义类扩展另一个类
如果您正在编写代码,这取决于存在的核心类的子类,那么该子类将成为项目中至关重要的部分.如果您不能更改核心本身,则将该类添加为依赖项.就这么简单.

I'm not quite sure I understand your question, but the answer should be self-evident: If your custom model extends from the core model, you can simply extend another class from that custom class
If you are writing code, that depends on a child of the core class being present, then that child class becomes a vital part of your project. If you can't change the core itself, add that class as a dependency. It's as simple as that.

添加第二层继承不必担心,这样做是很常见的.这样的事情是完全可以预测和可靠的:

Adding a second layer of inheritance needn't worry you, it's perfectly common to do so. Something like this is perfectly predictable, and reliable:

namespace Core;
class Model
{
    public function coreTest()
    {
        return 'from the core';
    }
}
namespace Custom;
use Core\Model;
class CustomModel extends Model
{
    public function customTest()
    {
        return 'from the custom model';
    }
}
//finally
namespace Project;
use Custom\CustomModel;
class ProjectModel extends CustomModel
{
    public function test()
    {
        return array(
            $this->coreTest(),
            $this->customTest(),
            'From the project'
        );
    }
}
$test = new ProjectModel();
echo implode(PHP_EOL, $test->test());

但是,如果您希望给定的类从另一个类扩展,则根据该类是否存在来寻找条件导入.
一个简单的use语句在编译时进行评估,因此您无法使用if检查在您从哪个扩展类之间进行切换.

If, however you want a given class to extend from another class, based on whether or not that class exists, you are looking for conditional imports.
A simple use statement is evaluated at compile-time, so there's no way you can use an if check to switch between which class you extend from.

尽管有一个变通的解决方法,但我不会依靠它.检查给定的类是否存在(没有自动加载),并为存在的类设置别名.

There is, however a hacky work-around, but I wouldn't rely on it. Check if the given class exists (without autoloading), and set an alias to the class that does.

if (!class_exists('\\Custom\\Model', false))
    class_alias('\\Core\\Model', 'Base');
else
    class_alias('\\Custom\\Model', 'Base');
class CustomModel extends Base
{}

但是真的:不要走这条路.当然,您的代码可以正常工作,但是如果您依赖自定义类中定义的可用方法,但是缺少该类,那么您的代码将失败...可怕.

But really: don't go down this route. Sure your code will work, but if you then rely on a method being available, that was defined in the custom class, but that class was missing, then your code will fail... horribly.

有关条件进口的详细信息:

Details on conditional imports:

为什么使用类别名酶?

这篇关于PHP命名空间覆盖Use语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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