简体   繁体   English

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

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

I set up all the basic setup for Codeigniter Framework. 我为Codeigniter Framework设置了所有基本设置。

My Code: 我的代码:

controllers/index.php: controllers / index.php:

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

controllers/user/file_name.php: //In my case 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...
        }
    }

Output: What I getting: 输出:我得到的是:

welcome

What I need: 我需要的:

welcome this is index

My problem is I have file_path instead of file_name in include, so I am unable to create Object for the file.? 我的问题是在include中有file_path而不是file_name,所以我无法为该文件创建对象。

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

In your index.php file: 在您的index.php文件中:

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

In your file_name.php file change: 在您的file_name.php文件中进行更改:

function index()

to: 至:

public function index ()

if your trying to include a file, be sure to remember that your using a framework, CI. 如果您尝试包含文件,请务必记住您使用的是CI框架。 the proper way of including a file using codeigniter is using the "$this->load->view('name of file');" 使用codeigniter包含文件的正确方法是使用“ $ this-> load-> view('文件名');” method, because , there are instance that using the include cause an error because it use preferably for pure php style not framework style. 方法,因为,存在使用include导致错误的实例,因为它最好用于纯PHP样式而不是框架样式。

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

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