如何使用include / require使用php访问该类?

时间:2015-02-27 12:39:13

标签: php codeigniter include

我为Codeigniter Framework设置了所有基本设置。

我的代码:

controllers / index.php:

include "file_name.php"; //In my case APPPATH . "controllers/user/user_data.php"

controllers / user / file_name.php://在我的案例中controller / user / user_data.php

<?php 

    echo "Welcome";

    class File_name 
    {
        function index()
        {
            echo "this is index";
            # code...
        }
    }

输出: 我得到了什么:

welcome

我需要什么:

welcome this is index

我的问题是我在include中有file_path而不是file_name,所以我无法为该文件创建Object。?

2 个答案:

答案 0 :(得分:1)

echo "Welcome ";
$fileName = new File_name();
$fileName->index(); 

答案 1 :(得分:1)

在index.php文件中:

include "file_name.php";
$file_name_obj = new File_Name();
$file_name_obj->index();

在你的file_name.php文件中更改:

function index()

为:

public function index ()