繁体   English   中英

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

[英]How to access the class using include/require using php?

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

我的代码:

controllers / index.php:

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

controllers / user / file_name.php://就我而言,controllers / 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,所以我无法为该文件创建对象。

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

在您的index.php文件中:

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

在您的file_name.php文件中进行更改:

function index()

至:

public function index ()

如果您尝试包含文件,请务必记住您使用的是CI框架。 使用codeigniter包含文件的正确方法是使用“ $ this-> load-> view('文件名');” 方法,因为,存在使用include导致错误的实例,因为它最好用于纯PHP样式而不是框架样式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM