codeigniter 在多个类别中拥有一个特质

4urapxun  于 2023-01-06  发布在  其他
关注(0)|答案(4)|浏览(135)

也许我没有得到整个特质系统,所以我想我会问StackOverFlow。
我的第一个特点是...

<?php
trait MY_Stat
{   
  var $dex;
  var $int;
  var $str;
}
?>

我不能设法使它与我的类(在另一个文件)无论如何工作...

class MY_Mobile
{
  use MY_Stat;

  public function __construct($params = NULL)
  {     
    var_dump($this);
  }
}

我总是碰壁:

Fatal error: Trait 'MY_Trait' not found in ...\wamp\www\game\application\libraries\MY_Mobile.php

我希望在许多类中都有这个trait,比如手机类、物品类等等......我是否应该把trait的定义和类放在同一个文件中?
顺便说一句,如果你使用codeigniter,你是如何设法让它加载的,你是把你的traits设置到一个帮助文件,库文件...?

o4hqfura

o4hqfura1#

你可以这样做来解决这个问题:

路径:

application/helpers/your-trait-class.php

内容:

<?php
if (!trait_exists('MY_Stat')) {
    trait MY_Stat
    {
        public $dex;
        public $int;
        public $str;
    }
}

在你的库中,在!defined('BASEPATH');行之后手动包含trait类(正如我所说的)。

您的媒体库:

include APPPATH . 'helpers/your-trait-class.php';

class YourLibrary {
    use MY_Stat;

并在库方法中使用trait类属性,如下所示:

$this->dex = 0.1;
$this->int = 1;
$this->str = 'Hello';
vcudknz3

vcudknz32#

另一个对我有效的替代方法是..其中model_base位于model文件夹中

@include_once ('model_base.php');  
class device_token extends CI_Model {

        // PHP 5.4 Traits
        use model_base;
wxclj1h5

wxclj1h53#

我从什么地方得到了这个答案,现在我不记得是从哪里得到的了。
在models文件夹中,创建一个名为trait_autoloader. php的文件

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

class trait_autoloader{
    public function __construct(){
        return $this->init_autoloader();
    }
    private function init_autoloader(){
        spl_autoload_register(function($classname){
            if(strpos($classname, 'trait') !== false){
                strtolower($classname);
                require('application/traits/'.$classname.'.php');
            }
        });
    }
}

现在你已经完成了它打开配置文件夹中的自动加载文件在应用程序文件夹,并把'trait_autoloader'在下面的行,

autoload['model'] = array( 'trait_autoloader' );

在应用程序文件夹中,创建一个名为traits的文件夹,并将您的traits放在该文件夹中,注意:我对该文件夹中的所有文件都使用“trait”前缀。在本例中,我使用了一个名为traitFunction.php的简单文件

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

trait traitFunction{
    public function FunctionName(){
       //Function Body
    }
}

现在你已经完成了。把下面的行放到你想要的任何模型类中,它们就会很好地工作。

use traitFunction;

请记住,此功能仅适用于模型。

7kqas0il

7kqas0il4#

为了利用spl_autoload_register,我不得不对codeigniter index.php文件做了一个小的修改
代码点火器索引. php

function load_traits($traits)
{
    $traits_found = [];
    $traitsBasePath = APPPATH .'traits';

    find_trait($traitsBasePath, $traits, $traits_found);
    
    foreach ($traits_found as $found)
        include_once $found;
}



function find_trait($dir, $searchForTrait, &$found)
{
    if(is_string($searchForTrait))
    {
        $dirHandle = opendir($dir);

        if($dirHandle !== FALSE)
        {
            while (false !== ($entry = readdir($dirHandle)))
                if($entry != '.' && $entry != '..')
                    if(is_dir($dir. '/' .$entry))
                        find_trait($dir. DIRECTORY_SEPARATOR .$entry, $searchForTrait, $found);
                    else
                        if(strpos($dir. DIRECTORY_SEPARATOR .$entry, 
                            str_replace("/", DIRECTORY_SEPARATOR, $searchForTrait . '.php')) !== FALSE)
                                array_push($found, $dir. DIRECTORY_SEPARATOR .$entry);
        }
        else
            die("Traits directory does not exists");
    }
    else if(is_array($searchForTrait))
    {
        $dirHandle = opendir($dir);

        if($dirHandle !== FALSE)
        {
            while (false !== ($entry = readdir($dirHandle)))
                if($entry != '.' && $entry != '..')
                    foreach ($searchForTrait as $sTrait) {
                        if(is_dir($dir. '/' .$entry))
                            find_trait($dir. DIRECTORY_SEPARATOR .$entry, $sTrait, $found);
                        else
                            if(strpos($dir. DIRECTORY_SEPARATOR .$entry, 
                                str_replace("/",
                                    DIRECTORY_SEPARATOR, 
                                        $sTrait . '.php')) !== FALSE)
                                            array_push($found, $dir. DIRECTORY_SEPARATOR .$entry);
                    }
                    
        }
        else
            die("Traits directory does not exists");
    }
    else
        die("Parameter must be string or array");
}

spl_autoload_register(function($traitname) {
    load_traits($traitname);
});

话虽如此,您必须在codeigniter application目录中创建一个traits文件夹,并在那里创建您的traits。
这个怎么用?
在任何控制器中,您只需按如下方式使用它

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

class Store extends CI_Controller {

    use test_trait;

}

use test_trait时,将在traits目录中搜索文件test_trait.php。如果找到该文件,将自动包含

相关问题