CodeIgniter中的自定义类 [英] Custom classes in CodeIgniter

查看:155
本文介绍了CodeIgniter中的自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于CodeIgniter的初学者来说,这似乎是一个非常普遍的问题,但是到目前为止,我发现的所有解决方案似乎都与我的问题无关。就像主题所说的那样,我正在尝试在CodeIgniter中包含一个自定义类。

Seems like this is a very common problem for beginners with CodeIgniter, but none of the solutions I've found so far seems very relevant to my problem. Like the topic says I'm trying to include a custom class in CodeIgniter.

我试图在下面创建该类的几个对象并将它们放置在数组中,因此我需要该类可用于模型。

I'm trying to create several objects of the class below and place them in an array, thus I need the class to be available to the model.

我试过使用CodeIgniter中的load(library-> load('myclass')函数),除了试图创建该类的对象外

I've tried using the load (library->load('myclass') functions within CodeIgniter which sort of works, except it tries to create an object of the class outside the model first. This is obviously a problem since the constructor expects several parameters.

到目前为止,我发现的解决方案是

The solutions I've found so far is


  1. 一个简单的php include看起来似乎还不错,但是由于我是
    CodeIgniter的新手,所以我想确保我坚持使用
    可能。

  2. 按照建议的这里,但是我不确定如何实现。

  1. A simple php include which seems fine enough, but since I'm new to CodeIgniter I want to make sure I'm sticking to it as much as possible.
  2. Creating a "wrapper class" as suggested here, however I'm uncertain how I would implement this.

我要包括的类
User.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class User{
    public $ID = 0;
    public $username = 0;
    public $access_lvl = 0;
    public $staff_type = 0;
    public $name = 0;    

    public function __construct($ID, $username, $access_lvl, $staff_type, $name) 
    {
        $this->ID = $ID;
        $this->username = $username;
        $this->access_lvl = $access_lvl;
        $this->staff_type = $staff_type;
        $this->name = $name;
    }

    public function __toString() 
    {
        return $this->username;
    }
}
?>

需要User.php的方法(模型)

function get_all_users()
{
    $query = $this->db->get('tt_login');
    $arr = array();

    foreach ($query->result_array() as $row)
    {
        $arr[] = new User
        (
            $row['login_ID'],
            $row['login_user'],
            $row['login_super'],
            $row['crew_type'],
            $row['login_name']
        );
    }    

    return $arr;
}

最后是控制器,

function index()
{
        $this->load->library('user');
        $this->load->model('admin/usersmodel', '', true);            

        // Page title
        $data['title'] = "Some title";
        // Heading
        $data['heading'] = "Some heading";
        // Data (users)
        $data['users'] = $this->usersmodel->get_all_users();


推荐答案

如果您的PHP版本> = 5.3,则可以使用命名空间自动加载功能。

If you have PHP version >= 5.3 you could take use of namespaces and autoloading features.

库文件夹中的简单自动加载器库。

A simple autoloader library in the library folder.

<?php
class CustomAutoloader{

    public function __construct(){
        spl_autoload_register(array($this, 'loader'));
    }

    public function loader($className){
        if (substr($className, 0, 6) == 'models')
            require  APPPATH .  str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php';
    }

}
?>

模型目录中的User对象。 (models / User.php)

The User object in the model dir. ( models/User.php )

<?php 
namespace models; // set namespace
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class User{
 ...
}

新用户... 新模型new用户(...)

function get_all_users(){
    ....
    $arr[] = new models\User(
    $row['login_ID'],
    $row['login_user'],
    $row['login_super'],
    $row['crew_type'],
    $row['login_name']
    );
    ...
}

在控制器中,只需确保像这样调用customautoloader:

function index()
{
        $this->load->library('customautoloader');
        $this->load->model('admin/usersmodel', '', true);            

        // Page title
        $data['title'] = "Some title";
        // Heading
        $data['heading'] = "Some heading";
        // Data (users)
        $data['users'] = $this->usersmodel->get_all_users();

这篇关于CodeIgniter中的自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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