PHP和动态名称空间

时间:2018-06-24 18:05:35

标签: php namespaces

Core / View / TestView.php:

<?php

  namespace Core\View;
  use Core\Components as Components;

  class TestView
  {

    public function renderView()
    {
      $Component=Components\TestComponent();
      echo $Component->renderComponent();
    }

  }

?>

Core / Components / TestComponent.php:

<?php

  namespace Core\Components;

  class TestComponent
  {

    public function renderComponent()
    {
      echo "Core component here."
    }

  }

?>

App / Components / TestComponent.php:

<?php

  namespace App\Components;

  class TestComponent extends \Core\Components\TestComponent
  {

    public function renderComponent()
    {
      echo "App component here."
    }

  }

?>

app.php:

<?php

  use App\Components as Components;

  class View extends \Core\View\TestView
  {
  }


  $View=new View();
  echo $View->renderView();


?>

是否有任何合适的方法来获取上述应用以在此处呈现“ App组件”-即,让扩展类使用扩展文件中的命名空间?还是通过工厂功能实现此目标的唯一方法?

0 个答案:

没有答案