PHP CodeIgniter 框架中的命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3700626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:45:53  来源:igfitidea点击:

Namespace in PHP CodeIgniter Framework

phpcodeigniternamespaces

提问by Tejas Patel

Does CodeIgniter support Namespace?

CodeIgniter 是否支持命名空间?

回答by Timothy Perez

How To Get Namespaces to Work in Codeigniter

如何让命名空间在 Codeigniter 中工作

Actually, you can get namespaces to work in conjunction to relative paths in your application models. This modification makes loading models much easier and also allows you to have interfaces...

实际上,您可以让命名空间与应用程序模型中的相对路径结合使用。这种修改使加载模型变得更加容易,并且还允许您拥有接口......

Add this to the end of your application/config/config.php

将此添加到 application/config/config.php 的末尾

spl_autoload_extensions('.php'); // Only Autoload PHP Files

spl_autoload_register(function($classname){

    if( strpos($classname,'\') !== false ){
        // Namespaced Classes
        $classfile = strtolower(str_replace('\','/',$classname));

        if($classname[0] !== '/'){
            $classfile = APPPATH.'models/'.$classfile.'.php';
        }               
        require($classfile);
    } else if( strpos($classname,'interface') !== false ){
        // Interfaces
        strtolower($classname);
        require('application/interfaces/'.$classname.'.php');
    }

});

Example Namespaced Class:

命名空间类示例:

<?php
// File: application/models/foo/bar.php
namespace foo;

class Bar extends \CI_Model implements \Awesome_interface {

    public $foobar;

    public function __construct() {
        return parent::__construct();
    }

    public function getFoobar() {
        return $this->foobar;
    }

    public function setFoobar($val) {
        $this->foobar = $val;
    }

}

Example Instantiation of Class in Your Code Somewhere:

在您的代码中某处的类实例化示例:

IMPORTANT NOTE:DO NOT USE BUILT IN CI_Loader ( Ex: $this->load->model(); )

重要提示:不要在 CI_Loader 中使用内置(例如: $this->load->model(); )

// This will Autoload Your Namespaced Class
$example = new foo\Bar();

or alternatively on top of your PHP class (ex: controller, other model), you can do this...

或者在您的 PHP 类(例如:控制器、其他模型)之上,您可以执行此操作...

<?php
...
use foo\Bar as FooBar;

...

// Then you can just do this
$example = new FooBar();

Example of Interface:

接口示例:

<?php
// File: application/interfaces/awesome_interface.php
interface Awesome_interface {

    public function getFoobar();

}

回答by adaxa

Namespace are supported by php and not by the framework (codeigniter in your case). If you use namespaces php version must be >= 5.3.0 Codeigniter dosen`t use namespaces because it is written to support php 4.

命名空间由 php 支持,而不是由框架(在您的情况下为 codeigniter)支持。如果您使用命名空间 php 版本必须 >= 5.3.0 Codeigniter 不使用命名空间,因为它是为支持 php 4 而编写的。

回答by Sunil Vaishnav

Just a simple psr-4 autoloading and you are done.

只是一个简单的 psr-4 自动加载,你就完成了。

In the config/config.phpload composer using

config/config.php加载作曲家中使用

$config['composer_autoload'] = FCPATH.'vendor/autoload.php';

In the root directory run composer installIn the generated composer.jsonadd following lines for psr4 autoloading.

在根目录运行composer install在生成的composer.json添加以下行用于 psr4 自动加载。

    "autoload": {
        "psr-4": {
            "App\": "application/"
        }
    },  

Appwould be your namespace in this case.

App在这种情况下将是您的命名空间。

Example: suppose you have a class Servicein libraries folder. You can namespace it with:

示例:假设您Service在库文件夹中有一个类。您可以使用以下名称命名它:

<?php
namespace App\libraries;

class Service{
}

Use it in Welcomecontroller class:

Welcome控制器类中使用它:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use App\libraries\Service;
class Welcome extends CI_Controller {
}

回答by Nick Tsai

You could check this out: yidas/codeigniter-psr4-autoload

你可以看看这个:yidas/codeigniter-psr4-autoload

The lib defines appas CI application root so that every classes in application could be loaded with PSR-4 namespace:

lib 定义app为 CI 应用程序根,以便应用程序中的每个类都可以使用 PSR-4 命名空间加载:

\app\libraries\MemberService::auth();
\app\helpers\ArrayHelper::indexBy($input);
\app\widgets\StatWidget::run();
class Blog_model extends app\core\BaseModel {}
class Car_model implements app\contracts\CarInterface {}

Sample code for defining a class:

定义类的示例代码:

<?php
namespace app\helpers;
class ArrayHelper
{
    public static function indexBy($input) {}
}

https://github.com/yidas/codeigniter-psr4-autoload

https://github.com/yidas/codeigniter-psr4-autoload